【问题标题】:junit: no tests foundjunit:没有找到测试
【发布时间】:2014-04-23 13:12:36
【问题描述】:

我继承了一个 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)

我不明白为什么我会收到一个归结为:“未找到测试”的错误。虽然我的项目结构不同,但操作系统上的文件夹结构匹配(这是另一件让我感到困惑的事情)。为什么会出现此错误,如何解决?

【问题讨论】:

    标签: java junit intellij-idea


    【解决方案1】:

    我也遇到了这个错误:

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

    原因是我忘了说明

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

    同步项目后,它找到了测试。所以也许它可以帮助别人。

    【讨论】:

    • 有没有办法结合这个 //testInstrumentationRunner "android.test.InstrumentationTestRunner"?
    • 仍然是一个有用的提示。对于 Jetpack 这将起作用:testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    【解决方案2】:

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

    public class EmailProviderTest {
    

    测试方法会在@Test注解中找到。

    阅读:JUnit confusion: use 'extend Testcase' or '@Test'?

    【讨论】:

    • 当我删除 extends TestCase 时,我收到一个编译时错误:java: cannot find symbol symbol: method assertTrue(boolean)。
    • 不同的问题:您缺少静态导入 org.junit.Assert.assertTrue
    • true,只需删除“extends TestCase()”即可运行测试!
    【解决方案3】:

    我也得到了这个:

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

    通过重命名method1

    中的测试方法解决
    @Test
    public void method1(){
        // Do some stuff...
    }
    

    testMethod1

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

    【讨论】:

    • 惊人的修复,没有一次可以想象这个。
    【解决方案4】:

    如果您使用的是 jUnit 4,则不要使用 extends TestCase,从而消除此固定错误。

    【讨论】:

    • 太棒了。它修复了
    【解决方案5】:

    我在 Intellij IDEA 中遇到了 Java 框架的同样问题。 我做得很好:

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

    但是我做错了一件事:方法的名称没有以“test”开头,其实是:

    @Test
    public void getSomethingTest(){
    

    当我把它改成:

    @Test
    public void testGetSomethingTest(){
    

    我已经解决:执行程序终于能够将此方法识别为测试方法。 我没有改变其他任何东西。

    【讨论】:

    • 在 Intellij 为我工作。
    【解决方案6】:

    我在创建private方法时遇到了JUnit 5的情况:

    import org.junit.jupiter.api.Test
    
    class Test1 {
    
        @Test
        private fun sort() {
            println("1")
        }
    }
    

    删除private

    【讨论】:

      【解决方案7】:

      我在使用数据提供者时遇到过,其中一个参数有一个换行符,如下所示:

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

      删除\n 解决了这个问题

      【讨论】:

        【解决方案8】:

        在我的例子中,我在任务 junit 的类路径中需要 junit4-version.jar 并且:

        ant-version.jar
        ant-junit-version.jar
        ant-junit4-version.jar
        

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

        我收到错误“junit.framework.AssertionFailedError: No tests found in ...”,而我没有在正确的位置放置 ant-junit4-*version*.jar

        我通过安装 ant-optional debian/ubuntu 包解决了这个问题:

        apt-get install ant-optional
        

        【讨论】:

        • 我只使用了 ant-junit-version.jar 和 ant-junit4-version.jar 就可以了,谢谢!
        【解决方案9】:

        我对此感到烦恼,直到我从该文件夹中移动测试源文件之前,没有一个答案对我有帮助:

        src/test/java/com/junit/test
        

        到这个文件夹:

        src/test/java/com/junit
        

        【讨论】:

          【解决方案10】:

          另一个原因通常我看到人们有方法参数,删除测试方法中可能有的任何参数,无论你添加 @Test 注释,你需要让你的方法名称以“test”开头,以便 Junit 选择你测试用例。 希望有帮助。

          【讨论】:

            【解决方案11】:

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

            name() 在源命名方法中。

            【讨论】:

              猜你喜欢
              • 2018-08-22
              • 1970-01-01
              • 2016-07-21
              • 2020-12-30
              • 2018-05-22
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多