The setUp method in android.test.AndroidTestCase is not mocking

I am trying to come to terms with the new unit test feature in Android Studio. I followed the instructions http://tools.android.com/tech-docs/unit-testing-support . In the description, the error "Method ... not maceded" is explicitly mentioned there and it is proposed to put the following in build.gradle :

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

This works because tests are run when launched from the command line using

 gradlew test --continue 

but not when I run the test class from Android Studio using rightclick β†’ run. So I get the same error again:

 java.lang.RuntimeException: Method setUp in android.test.AndroidTestCase not mocked. See https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support for details. at android.test.AndroidTestCase.setUp(AndroidTestCase.java) at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) 

Any ideas on how to solve this?

EDIT: the contents of the test class does not matter much because the setUp of the test does not work, I tried with the simplest class:

 public class ContactFormToolTest extends AndroidTestCase { public void testSOmething(){ assertEquals(false, true); } } 

Also tried overriding setUp, it doesn't matter.

+7
android android-studio unit-testing
source share
3 answers

The new Unit Tests feature in Android Studio fakes the entire Android SDK so that you can quickly run tests only in Java, without installing your application on an Android device (this is similar to Robolectric). The general idea is that you are mocking all the answers to Android SDK calls.

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

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

You need to choose one approach. If you need a clean unit test, then you should probably use the JUnit 4 test class instead of AndroidTestCase. More instructions here: https://developer.android.com/training/testing/unit-testing/local-unit-tests.html#build

+7
source share

From: https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support#TOC-Method-...-not-mocked.-

The android.jar file, which is used to run unit tests, does not contain any actual code that is provided on the Android system image on a real device. Instead, all methods throw exceptions (by default). This is to make sure that your unit is testing only your code and is not dependent on any specific behavior of the Android platform (you haven’t been explicitly mocking, for example, using Mockito). If this proves problematic, you can add the snippet below to your build.gradle to change this behavior:

 android { // ... testOptions { unitTests.returnDefaultValues = true } } 
+10
source share

Starting with SDK 24, AndroidTestCase is deprecated

This class is deprecated at API level 24.

Use InstrumentationRegistry instead. New tests should be written using the Android Testing Support Library .

It is intended to use the Espresso environment to test the user interface. There is a tutorial .

+4
source share

All Articles