Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 找到JUnit 0测试_Java_Testing_Junit - Fatal编程技术网

Java 找到JUnit 0测试

Java 找到JUnit 0测试,java,testing,junit,Java,Testing,Junit,我想编写一个简单的Testclass,其中我添加了两个数字: import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; public class ttest { private final ttest tT = new ttest(); public int add(int one, int two) { return one + two; } @Test

我想编写一个简单的Testclass,其中我添加了两个数字:

import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

public class ttest {

  private final ttest tT = new ttest();

  public int add(int one, int two) {
    return one + two;
  }

  @Test
  public void eq() {
    int result = tT.add(1, 2);
    assertEquals(result, 3);
    assertTrue(result == 3);
  }
}
它编译正确,但如果我在控制台中运行测试,它会显示:

├─ JUnit Jupiter ✔
└─ JUnit Vintage ✔
[         2 containers found      ]
[         2 containers successful ]
[         0 tests found           ]
它找到了所有容器,但没有找到添加数字的Testcase。 我使用课程中的脚本开始测试运行,因此问题必须在.java中

我在控制台中使用的脚本是:

javac --class-path="junit-platform-console-standalone-1.6.2.jar" ttest*.java
java -jar junit-platform-console-standalone-1.6.2.jar --class-path="." --scan-class-path

我不知道这对你来说是否是一个可接受的解决方案。使用以下代码,它对我有效:

import org.junit.Assert;
import org.junit.Test;

public class ttest {

    //private final ttest tT = new ttest(); 

    public int add(int one, int two) {
        return one + two;
    }

    @Test
    public void eq() {
        int result = add(1, 2); 
        Assert.assertEquals(result, 3);
        Assert.assertTrue(result == 3);
    }
}

例如,可以通过将类重命名为TTest来运行您的示例

根据官方文件:

-n、 -include classname提供一个正则表达式,仅包含其完全限定名匹配的类。 为了避免不必要地加载类,仅使用默认模式 包括以一个或多个测试结尾的类名。当选择此选项时 如果重复,所有模式将使用或语义进行组合。 默认值:^.*测试$


所以简单地说,您的类与默认模式不匹配,因此未被框架选中。

什么是@Test的完全限定类名?换句话说,您为@Test导入了什么?您使用的是Spring Boot吗?如果是这样,您必须使用spring boot测试注释对测试进行注释,比如@RunWithSpringRunner.class@SpringBootTestwebEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT。如果您没有使用spring boot,那么我想您仍然需要设置@RunWithannotation@Mureinik我通过org.junit.jupiter.api.*导入了注释类型测试;我犯了一个错误,没有将第一个导入行复制到堆栈Overflow@FedericoPiazza我没有用弹簧靴。在课堂上,我们也没有使用@RunWith注释,我们的测试也在那里进行。所以我认为这不会是问题所在?@fid,你能告诉我你是如何调用/运行junit的吗?它给出了相同的报告,发现了0个测试。这没有意义,这是junit 4,而OP给出了junit 5的示例。此外,如果你不知道为什么要发帖——那就评论吧。。。