I have applied the following instructions: https://code.google.com/p/android-test-kit/wiki/AndroidJUnitRunnerUserGuide to setup JUnit4 tests.
In particular I am following the steps to use Junit4 with ActivityInstrumentationTestCase2 however when I run tests in Android Studio the @Test annotated methods are not being executed. If I prefix methods with 'test' it works however this is not what I expect for the JUnit 4 tests.
Has anyone encountered this problem?
Regards,
When you use AndroidJUnitRunner to run your tests, you can access the context for the app under test by calling the static ApplicationProvider. getApplicationContext() method. If you've created a custom subclass of Application in your app, this method returns your custom subclass's context.
Earlier in the module's build.gradle file, we have a testInstrumentationRunner statement: testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" (from ToDoTests/build.gradle) This tells Android how to run our JUnit instrumented tests. JUnit uses “runner” classes for this role, and androidx.
As the name suggests, Test Runner is a tool that is used to run or execute tests and export results. It is a library that selects the source code directory and picks the test files to run them for verifying bugs and errors.
This rule provides functional testing of a single Activity .
There is no need to extend ActivityInstrumentationTestCase2
anymore. If you need access to an Activity
, in the test support libraries ActivityTestRule
exists. Instead of extending ActivityInstrumentationTestCase2
you can now do:
@RunWith(AndroidJUnit4.class)
public final class ActivityTest {
@Rule
public final ActivityTestRule<MainActivity> activityTestRule =
new ActivityTestRule<>(MainActivity.class);
@Test
public void useActivityInTest() {
Activity activity = activityTestRule.getActivity();
}
}
I found I had to add a single test method like this:
public void test() {}
Then all of my real tests are annotated with @Test
Fixed when specified in the file app/build.gradle the test-runner:
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
defaultConfig {
applicationId "com.domain.appname"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With