获取junit.framework.AssertionFailedError:使用Unit和Mockito时,[package]中没有找到任何测试 [英] Getting junit.framework.AssertionFailedError: No tests found in [package] when using Unit and Mockito
问题描述
我在我的android项目中添加了以下依赖项:
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'
使用junit4 api创建一个测试(例如,Adder是一个简单的类,它总结了int):
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:
运行测试
测试运行已开始
junit.framework.AssertionFailedError:在com.test.myapp中找不到测试.AdderTest
在android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
在android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
在android.test.InstrumentationTestRunner .onStart(InstrumentationTestRunner.java:554)
在android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:1701)
完成
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?
推荐答案
这些不是单元测试,它们是仪器测试。我有同样的问题并通过将这些行添加到模块的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.
这篇关于获取junit.framework.AssertionFailedError:使用Unit和Mockito时,[package]中没有找到任何测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!