Ascorbin Ascorbin - 1 year ago 667
Android Question

Method setUp in android.test.AndroidTestCase not mocked

I'm trying to come to terms with the new unit test feature of Android Studio.
I've followed the instructions on The description there explicitly mentions the 'Method ... not mocked' error and suggests to put the following into the


android {
// ...
testOptions {
unitTests.returnDefaultValues = true

This works in so far as the tests run when started from the command line with

gradlew test --continue

but not when I run the test class from Android Studio with rightclick -> run. This way, I get the same error again:

java.lang.RuntimeException: Method setUp in android.test.AndroidTestCase not mocked. See for details.
at android.test.AndroidTestCase.setUp(
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(
at com.intellij.rt.execution.junit.JUnitStarter.main(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
at com.intellij.rt.execution.application.AppMain.main(

Any ideas on how to solve this?

EDIT: The content of the test class doesn't really matter because the setUp of the test fails, I tried with the most simple class:

public class ContactFormToolTest extends AndroidTestCase {

public void testSOmething(){
assertEquals(false, true);

Also tried overriding setUp, makes no difference.

Answer Source

The new Unit Tests feature in Android Studio fakes the entire Android SDK so that you can run fast, Java-only tests, without needing to install your application on an Android device (this is similar to Robolectric). The general idea is that you mock all the responses from the Android SDK calls.

AndroidTestCase is used to run a test with the real Android SDK.

So, your issue is that you are trying to run an AndroidTestCase that depends on the Android SDK, but your test runner is launching the Unit Tests environment, which uses a fake Android SDK instead of a real one.

You need to choose one approach. If you want a pure unit test, then you probably should use a JUnit 4 test class instead of an AndroidTestCase. More instructions here: