maven - 如何在eclipse中使用maven junit5框架项目测试java项目中的方法
问题描述
我创建了一个新的 maven junit5 框架项目来测试现有的 java 项目。我在新创建的 maven junit5 框架项目的构建路径中添加了 java 项目。我右键单击要为其添加 junit 测试用例的方法并选择新的 junit 测试用例并将源文件夹更改为新的 maven junit5 框架项目 src 目录,并将其余选项保留为默认值。创建了 junit 测试并将测试作为单元测试运行,没有任何问题(下面的屏幕截图)。使用 maven 运行相同的测试得到以下错误。我在 pom(below) 中添加了 surefire 插件,但仍然出现以下错误。使用日食。
-------------------------------------------------------------------------------
Test set: com.build.VersionInfoTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.003 s <<< FAILURE! - in com.build.VersionInfoTest
com.build.VersionInfoTest Time elapsed: 0.002 s <<< ERROR!
java.lang.NoClassDefFoundError: Lcom/build/VersionInfo;
Caused by: java.lang.ClassNotFoundException: com.build.VersionInfo
<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>UnitTesting</groupId>
<artifactId>com.unit.testing</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>com.unit.testing</name>
<description>Junit Tests</description>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.jupiter.version>5.5.2</junit.jupiter.version>
<junit.platform.version>1.5.2</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.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.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>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
</project>
更新:我清理了 pom(下)但现在没有发现测试?当我用junit运行项目时,会发现测试吗?
[INFO] Scanning for projects...
[INFO]
[INFO] --------------< UnitTesting:com.unit.testing >---------------
[INFO] Building com.unit.testing 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------- -----------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ com.unit.testing ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ com.unit.testing ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default- testResources) @ com.unit.testing ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ com.unit.testing ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @ com.unit.testing ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.build.VersionInfoTest
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 s - in com.build.VersionInfoTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.955 s
[INFO] Finished at: 2020-03-09T10:00:22-04:00
[INFO] ------------------------------------------------------------------------
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
<junit.jupiter.version>5.6.0</junit.jupiter.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
</plugins>
</build>
package com.dbb.build
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import com.dbb.build.VersionInfo;
class VersionInfoTest {
VersionInfo versionInfo = VersionInfo.getInstance();
@Test
void getVersion() {
String version = versionInfo.getVersion();
System.out.println(version);
assertNotNull(versionInfo.getVersion(), "expected a return value of"+version+"but was null");
}
}
更新:
[INFO] --- maven-resources-plugin:2.6:testResources (default- testResources) @ DBB-Unit-Testing ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ DBB-Unit-Testing ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Unit-Testing/target/test-classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Unit-Testing/src/test/java/com/VersionInfoTest.java:[7,25] cannot find symbol
symbol: class VersionInfo
location: package com.build
[ERROR] /Unit-Testing/src/test/java/com/build/VersionInfoTest.java: [11,9] cannot find symbol
symbol: class VersionInfo
location: class com.build.TestVersionInfo
[ERROR] /Unit-Testing/src/test/java/com/ /build/VersionInfoTest.java: [11,35] cannot find symbol
symbol: variable VersionInfo
location: class com.build.TestVersionInfo
[INFO] 3 errors
解决方案:使用 junit-platform-console-standalone-1.5.2.jar 并通过命令行运行单元。看起来如果我们有一个非 maven 项目 junit-platform-console-standalone 似乎是一个更好的选择。
解决方案
这是我在 Maven 3.x 中使用的一些 pom 的示例,以及在 Eclipse 中使用 JUnit 5 按预期执行的测试,也可以从命令行执行:
不要添加太多 Juniper 工件,如果存在一些会产生一些副作用。
另请注意,surefire 插件的更新版本在过去与 JUnit5 有一些问题
<properties>
<!-- ensure proper encoding of source and resource files in the project -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit-5.version>5.6.0</junit-5.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit-5.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
</plugins>
</build>
更新:
你可以在这里找到一个小例子:https ://gist.github.com/asa-git/8e34bbc51b5fcb09b7fab3efdaaa73c9
请注意,我使用的是 maven 版本 3.6.3 和 JDK 8。
此外,在 Windows 上从命令行运行时(但在其他系统上也是如此),您需要确保您的 JDK 在您的系统上安装任何其他 JSE 之前位于您的路径上。
推荐阅读
- google-cloud-platform - Google Cloud Platform 上的 GPU 实例在我终止后是否会得到维护?
- python - 由于heroku数据库刷新(django后端)而丢失用户输入数据
- javascript - 在 vuejs、vuex 中获取 406(不可接受的错误)
- javascript - 处理/计算两个对象值并从中生成输出
- c++ - 面临进程退出并返回值 3221225725 的问题
- authentication - 我应该如何为 Electron 桌面应用程序实现用户身份验证/角色?
- html - 网页尺寸太宽
- java - 不关闭 PrintWriter 对象的后果
- c++ - 是否可以组合部分模板特化来生成隐式生成的共享代码路径?
- javascript - 使用react时onChange和value有什么关系?