I\'m trying to come to terms with the new unit test feature of Android Studio. I\'ve followed the instructions on http://tools.android.com/tech-docs/unit-testing-support. The d
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: https://developer.android.com/training/testing/unit-testing/local-unit-tests.html#build
From: https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support#TOC-Method-...-not-mocked.-
The android.jar file that is used to run unit tests does not contain any actual code - that is provided by the Android system image on real devices. Instead, all methods throw exceptions (by default). This is to make sure your unit tests only test your code and do not depend on any particular behaviour of the Android platform (that you have not explicitly mocked e.g. using Mockito). If that proves problematic, you can add the snippet below to your build.gradle to change this behavior:
android {
// ...
testOptions {
unitTests.returnDefaultValues = true
}
}
As of SDK version 24, AndroidTestCase is deprecated
This class was deprecated in API level 24.
Use InstrumentationRegistry instead. New tests should be written using the Android Testing Support Library.
You are supposed to use the Espresso framework for UI testing. There is a tutorial.