【问题标题】:Getting junit.framework.AssertionFailedError: No tests found in [package] when using Unit and Mockito获取 junit.framework.AssertionFailedError: No tests found in [package] when using Unit and Mockito
【发布时间】:2015-08-07 00:49:38
【问题描述】:

我在我的 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'

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

@RunWith(MockitoJUnitRunner.class) 
public class AdderTest {

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

当我尝试运行测试时,我得到:

运行测试 试运行开始 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) 完成

我阅读了herehere,但没有任何帮助。

有人看到我做错了什么/有什么意见吗?

【问题讨论】:

  • 单元测试属于test文件夹,所以使用testCompile。此外,您不需要@RunWith(MockitoJUnitRunner.class)
  • 这只是一个例子。我确实需要在我的原始项目中使用模拟。此外,我正在测试的代码使用 android.util.Patterns.EMAIL_ADDRESS,当我将它作为单元测试运行时它返回 null。如果我将它作为 Android 仪器测试运行,它不会返回实际的匹配器吗?
  • 为了清楚起见,发布您的代码。您应该使用 Robolectric 进行单元测试。它将检测您需要的类,因此您不需要模拟所有内容
  • 我确实需要使用 Mockito 来模拟一些类依赖的响应。在测试类中使用 @RunWith(RobolectricTestRunner.class) + 添加 MockitoAnnotations.initMocks(this);我的 setup() 方法确实使我的测试作为单元测试工作(android.util.Patterns.EMAIL_ADDRESS 不返回 null)。这是要走的路吗?看起来很复杂:/
  • 一点也不复杂。请在此处查看我的 Android Gradle 模板:github.com/jaredsburrows/AndroidGradleTemplate/blob/master/…

标签: java android unit-testing junit mockito


【解决方案1】:

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

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

你必须在依赖项中添加跑步者,我建议你浓缩咖啡:

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

编辑:

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

MockitoAnnotations.initMocks(this);

或:

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

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

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

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

【讨论】:

  • @IgorGanapolsky 您要查找的行在app/build.gradle 中。这似乎支持需要testInstrumentationRunner 指令的说法。
【解决方案2】:

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

您需要恢复为手动调用MockitoAnnotations.initMocks(),并可选择取消调用Mockito.validateMockitoUsage()。当然,直接调用Mockito.mock(等等)仍然可以。

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

【讨论】:

  • 其实现代 Android gradle 项目的默认运行器现在是 Junit 4。
  • @IgorGanapolsky 当然,但前提是您将仪器测试运行器设置为 Szymon 的答案。内置的runner是JUnit3.8,所以还是需要override的;一旦它被覆盖,您将使用我的答案中的 Android 支持测试库。
猜你喜欢
  • 2016-10-16
  • 2017-05-13
  • 2017-02-14
  • 1970-01-01
  • 1970-01-01
  • 2022-12-01
  • 1970-01-01
相关资源
最近更新 更多