Getting junit.framework.AssertionFailedError: No tests found in [package] when using Unit and Mockito

11,443

Solution 1

These aren't unit tests, they are instrumentation test. I had the same problem and solved it by adding these lines to module's build.gradle:

android {
    ...
    sourceSets {
        ...
        defaultConfig {
            testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
        }
    }
}

You have to add the runner in dependencies, i suggest you espresso:

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'

EDIT:

I have forgotten about the annotation. Just remove @RunWith and make a @Before method with line:

MockitoAnnotations.initMocks(this);

or:

System.setProperty("dexmaker.dexcache", InstrumentationRegistry.getTargetContext().getCacheDir().getPath());

I have to mention this way is not recommended. To use the second one, you have to add dependencies with dex-maker for mockito:

androidTestCompile ('com.google.dexmaker:dexmaker-mockito:1.2') {
        exclude module: 'mockito-core'
    }

Since you have added mockito, we have to exclude mockito core from the new dependency. It already contains the normal dex-maker module.

Solution 2

You're using an Android Instrumentation test. By default, the Android test runner doesn't run on JUnit4, it runs on JUnit3 using subclasses of InstrumentationTestCase.

You'll need to revert to manual calls to MockitoAnnotations.initMocks(), with an optional tear-down call to Mockito.validateMockitoUsage(). Of course, calls directly to Mockito.mock (and such) will still work.

As an alternative, there is an official JUnit4 runner, which can be installed from the Android Support Test Library. By invoking this Instrumentation rather than the default test runner, you can run JUnit4 tests on your device, including using MockitoJUnitRunner or MockitoRule.

Share:
11,443
dors
Author by

dors

Updated on June 05, 2022

Comments

  • dors
    dors almost 2 years

    I added the following dependencies to my android project:

     // Unit testing dependencies
    androidTestCompile 'junit:junit:4.12'
    // Set this dependency if you want to use Mockito
    androidTestCompile 'org.mockito:mockito-core:1.10.19'
    

    And create a test using junit4 api (an example, Adder is a simple class that sums ints):

    @RunWith(MockitoJUnitRunner.class) 
    public class AdderTest {
    
        @Test
        public void testValidAdd() {
            Adder adder = new Adder();
            assertEquals(adder.add(1,1), 2);
        }
    }
    

    When I try to run the test, I get:

    Running tests Test running started junit.framework.AssertionFailedError: No tests found in com.test.myapp.AdderTest at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701) Finish

    I read here and here, but nothing helps.

    Does anyone see what I'm doing wrong / have any input?