java 获取 junit.framework.AssertionFailedError:使用 Unit 和 Mockito 时在 [package] 中找不到测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31868008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:16:29  来源:igfitidea点击:

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

javaandroidunit-testingjunitmockito

提问by dors

I added the following dependencies to my android project:

我在我的 android 项目中添加了以下依赖项:

 // 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):

并使用 junit4 api 创建一个测试(例如,Adder 是一个对整数求和的简单类):

@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

运行测试 测试运行开始 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) 在 android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554) 在 android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701) 完成

I read hereand here, but nothing helps.

在这里这里阅读,但没有任何帮助。

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

有没有人看到我做错了什么/有任何意见?

回答by Szymon Ga??zka

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:

这些不是单元测试,它们是仪器测试。我遇到了同样的问题并通过将这些行添加到模块的 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:

我忘记了注释。只需删除 @RunWith 并使用以下行创建 @Before 方法:

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:

我不得不提这种方式是不推荐的。要使用第二个,您必须使用 dex-maker 为 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.

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

回答by Jeff Bowman

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.

您正在使用 Android Instrumentation 测试。默认情况下,Android 测试运行器不在 JUnit4 上运行,它使用InstrumentationTestCase 的子类在 JUnit3 上运行。

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.

您需要恢复到手动调用MockitoAnnotations.initMocks(),以及可选的拆卸调用Mockito.validateMockitoUsage()。当然,直接调用Mockito.mock(等)仍然有效。

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 MockitoJUnitRunneror MockitoRule.

作为替代,有一个官方的 JUnit4 runner,可以从Android Support Test Library安装。通过调用此 Instrumentation 而不是默认测试运行器,您可以在您的设备上运行 JUnit4 测试,包括使用MockitoJUnitRunnerMockitoRule