我在使用 JUnit 5.52 时遇到问题,当我添加测试类并尝试运行它时,我收到了下一条消息:
The <classpath> or <modulepath> for <junit> must include junit.jar if not in Ant's own classpath BUILD FAILED (total time: 0 seconds)
我正在运行 Apache NetBeans IDE 11.3,我通过删除 JUnit 5 和使用 JUnit 4.12 来解决它,但由于大学要求,我可能会使用 5.52。
知道如何解决它,仍然使用 JUnit 5.52 或任何 JUnit 5 库吗?
提前致谢。
最佳答案
您可以在 https://github.com/junit-team/junit5-samples/tree/main/junit5-jupiter-starter-maven 找到有关如何在 Netbeans 中运行 Junit 5 的示例代码
您必须在 POM 中做两件事:
- 包括 Junit 5 库
- 在构建中包含一个足够新的 maven-surefire-plugin 版本
这是我测试过的有效配置:
<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>com.stackoverflow.example</groupId>
<artifactId>junit5</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.8.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
</plugins>
</build>
</project>
关于java - Netbeans 中的 JUnit 5 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61861041/