Junit:未找到测试

74

我继承了一个Java项目,对Java开发还很陌生。我感觉写一些代码测试可以让我更加熟悉这个项目的代码。我用IntelliJ编写我的代码。

我的现有项目的文件夹结构如下:

/myProject
  /src
    /main
      /java
        /com.lexcorp
          /core
            /email
              /providers
                emailProvider.java

我创建了一个新项目,用于保存此项目的测试。我希望这个项目既包含单元测试,也包含集成测试。目前,我的新项目的结构如下:

/myProjectTests
  /src
    /main
      /java
        /com.lexcorp.core.email.providers
          emailProviderTest.java

emailProviderTest.java文件的样子如下:

package com.lexcorp.core.email.providers;

import junit.framework.TestCase;
import org.junit.Test;

public class EmailProviderTest extends TestCase {

    private final String username = "[testAccount]";

    private final String password = "[testPassword]";

    @Test
    public void thisAlwaysPasses() {
        assertTrue(true);
    }
}

该项目有一个带以下属性的运行/调试配置:

  • 测试类型:所有包中的测试
  • 搜索测试:整个项目中搜索

当我运行此配置时,出现错误:

junit.framework.AssertionFailedError: No tests found in com.lexcorp.core.email.providers.EmailProviderTest
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
    at org.junit.runners.Suite.runChild(Suite.java:127)
    at org.junit.runners.Suite.runChild(Suite.java:26)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

我不明白为什么会出现这样的错误:"未找到测试"。虽然我的项目结构不同,但操作系统上的文件夹结构相匹配(这也让我感到困惑)。我为什么会得到这个错误,该如何解决?

11个回答

156

我也遇到了这个错误:

junit.framework.AssertionFailedError: 在...中找不到测试

原因是我忘记指定

defaultConfig {
    ...
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

同步项目后,它发现了测试。也许对其他人有帮助。


有没有办法与这个//testInstrumentationRunner "android.test.InstrumentationTestRunner"结合起来? - powder366
15
仍然是个有用的提示。对于Jetpack,这将起作用: testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" - wtk

44

junit.framework.TestCase的扩展是实现测试用例的旧JUnit 3方法,因为没有以字母test开头的方法,所以此方法不起作用。由于您正在使用JUnit 4,只需将类声明为

public class EmailProviderTest {

测试方法将从@Test注释中找到。

阅读:JUnit的困惑:使用'extend Testcase'还是'@Test'?


当我移除extends TestCase时,会出现编译时错误,显示:java: 找不到符号符号: 方法assertTrue(boolean)。 - JQuery Mobile
1
不同的问题:您缺少静态导入org.junit.Assert.assertTrue - Reimeus
只需删除“extends TestCase()”,测试就可以运行! - Tonnie

29

我也遇到了这个问题:

junit.framework.AssertionFailedError: No tests found in ...

通过将测试方法从method1重命名解决。

@Test
public void method1(){
    // Do some stuff...
}

测试 testMethod1

@Test
public void testMethod1(){
    // Do some stuff...
}

3
惊人的修复,没有人能够想象。 - Joy
实际上,这是一种已记录的行为。然而,自JUnit4以来,它已经过时了。 - arkascha

11
如果您正在使用jUnit 4,则不要使用“extends TestCase”,删除此内容可以解决错误。

1
太棒了。它修复了。 - Salvatore Pannozzo Capodiferro

9

我在使用Java框架的Intellij IDEA中遇到了同样的问题。

  • 我的类继承自TestCase
  • 我已经导入了junit.framework.TestCase
  • 我添加了@Test注解

但我做错了一件事:方法名没有以"test"开头,实际上是:

@Test
public void getSomethingTest(){

当我将其改为:

@Test
public void testGetSomethingTest(){

我已解决:执行程序最终能够将此方法识别为测试方法。我没有修改任何其他内容。


1
在Intellij中对我有用。 - Ali Ait-Bachir

2

我在创建一个私有方法时,在JUnit 5中遇到了这种情况:

import org.junit.jupiter.api.Test

class Test1 {

    @Test
    private fun sort() {
        println("1")
    }
}

移除 private


1
在我的情况下,我需要将junit4-version.jar加入到任务junit的类路径中,并且还需要:
ant-version.jar
ant-junit-version.jar
ant-junit4-version.jar

在我的ant安装库中(/usr/share/ant/lib)。

当我没有将ant-junit4-*version*.jar放在正确的位置时,我遇到了错误"junit.framework.AssertionFailedError: No tests found in ..."。

我通过安装debian/ubuntu软件包ant-optional来纠正这个问题:

apt-get install ant-optional

我刚刚使用了ant-junit-version.jar和ant-junit4-version.jar,然后它就可以正常工作了,谢谢! - Mohamed AbdElRazek

0

我对此感到很烦恼,没有任何答案能帮助我,直到我将测试源文件从这个文件夹移动:

src/test/java/com/junit/test

到这个文件夹为止:

src/test/java/com/junit

0

我在使用数据提供程序时遇到了问题,其中一个参数包含了换行符,就像这样:

@DataProvider
public static Object[][] invalidAdjustment() {
    return new Object[][]{
            {"some \n text", false},
            };
}

移除 \n 解决了这个问题。


-1

测试方法名称 public void testName() {}

其中 name() 在源命名中命名方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接