我刚刚开始学习Junit,并创建了一个非常简单的测试类,但是我遇到了两个问题:1)不能直接作为JUnit测试运行(我必须为我尝试在eclipse idk中运行的每个JUnit文件运行配置,这是为什么) ii)我跟踪了同一问题的几个答案,但没有帮助--使用测试运行器JUnit 5找不到测试。
我尝试过添加依赖项、重新启动、类似帖子中提供的其他解决方案
package io.javatest;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class MathUtilsTest {
@Test
void test() {
System.out.println("This test ran");
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.javatest</groupId>
<artifactId>junit-5-basics</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>junit-5</name>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<junit.jupiter.version>5.4.0</junit.jupiter.version>
<junit-platform.version>1.2.0</junit-platform.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>`enter code here`
</dependencies>
</project>发布于 2019-06-20 00:09:51
这是一个使用maven的junit 5项目的工作示例,并进行了示例测试。在命令行中使用"mvn test“运行测试。
发布于 2021-02-20 06:37:58
在您的IDE中,右键单击junit测试用例,选择"Run as“,然后单击"Run configurations",选择配置并向下滚动,将"Test Runner”更改为junit 4。现在应用并关闭并运行。这为我解决了这个问题。但想知道根本原因。
https://stackoverflow.com/questions/56648914
复制相似问题