且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取junit.framework.AssertionFailedError:使用Unit和Mockito时,[package]中没有找到任何测试

更新时间:2023-11-19 23:22:58

这些不是单元测试,它们是仪器测试。我有同样的问题并通过将这些行添加到模块的build.gradle来解决它:

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'

编辑:

我忘记了注释。只需删除@RunWith并使用line创建一个@Before方法:

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

MockitoAnnotations.initMocks(this);

或:

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

我不推荐这种方式。要使用第二个,你必须使用dex-maker为mockito添加依赖项:

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'
    }

由于你添加了mockito,我们必须从新的依赖项中排除mockito核心。它已包含正常的dex-maker模块。

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