Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AndroidJunit4.class runner in Android Studio does not invoke @Test methods

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,

like image 965
user3521637 Avatar asked Feb 07 '15 15:02

user3521637


People also ask

Why do you use AndroidJUnitRunner when running UI test?

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.

What is testInstrumentationRunner Androidx test runner AndroidJUnitRunner?

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.

What is the use of test runner?

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.

What is activity test rule?

This rule provides functional testing of a single Activity .


3 Answers

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();
    }
}
like image 131
Luke Avatar answered Oct 25 '22 04:10

Luke


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

like image 35
ericharlow Avatar answered Oct 25 '22 04:10

ericharlow


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"
}
like image 32
Sergey Smolnikov Avatar answered Oct 25 '22 05:10

Sergey Smolnikov