Maven 项目中的 JUnit 5 测试可在 IntelliJ 中运行,但不能在命令行中运行
JUnit 5 tests in Maven Project works in IntelliJ but Not from Command Line
使用 Java 1.8 和 JUnit 1.5,我创建了一个数独求解器,它从我基于 maven 的项目的资源目录中读取有效和无效文件。问题是,当我 运行 在 Intellij IDEA Ultimate Edition 中进行 JUnit 测试时,单元测试 运行 并且一切正常!但是,当我从命令行 运行 使用以下命令时 mvn clean test
或 mvn test
,none 的测试 运行!
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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.sudoku</groupId>
<artifactId>sudoku</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
项目结构:
sudoku
│
├── sudoku.iml
├── src
│ ├── test
│ │ ├── resources
│ │ │ ├─ valid.csv
│ │ └──── invalid.csv
│ │ └── java
│ │ └── com
│ │ └── sudoku
│ │ └── SudokuTest.java
│ └── main
│ ├── resources
│ │ ├── valid.csv
│ │ └── invalid.csv
│ └── java
│ └── com
│ └── sudoku
│ └── Sudoku.java
├── pom.xml
Sudoku.java:
package com.sudoku;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Arrays;
public class Sudoku {
public static final int SIZE = 9;
private final int[][] board = new int[SIZE][SIZE];
public boolean isValidRow() {
for (int x = 0; x < SIZE; x++) {
if (!isValidBoard(board[x])) {
System.out.println("Invalid row: " + Arrays.toString(board[x]));
return false;
}
}
return true;
}
public static boolean isValidBoard(int[] boardMatrix) {
return Arrays.stream(boardMatrix).sum() == 45;
}
public void loadCsvFile(String csvFile) throws Exception {
URL resource = getClass().getClassLoader().getResource(csvFile);
BufferedReader csvReader = new BufferedReader(new InputStreamReader(resource.openStream()));
int x = 0;
String row = null;
while ((row = csvReader.readLine()) != null) {
String[] rows = row.split(",");
int y = 0;
for (String singleRow : rows) {
if (singleRow != null && !"".equals(singleRow)) {
int rowIntegerValue = 0;
rowIntegerValue = Integer.parseInt(singleRow);
if (rowIntegerValue < 1 && rowIntegerValue > 9) {
throw new IllegalArgumentException(rowIntegerValue + " is invalid. Must be in between 1 - 9.");
}
board[x][y] = rowIntegerValue;
y++;
}
}
x++;
}
csvReader.close();
}
}
SudokuTest.java
package com.sudoku;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class SudokuTest {
@Test
public void isValidRow() throws Exception {
Sudoku sudoku = new Sudoku();
sudoku.loadCsvFile("valid.csv");
assertTrue(sudoku.isValidRow());
}
}
valid.csv:
9,2,3,4,5,6,7,8,1
4,5,6,7,8,9,1,2,3
7,8,9,1,2,3,4,5,6
5,3,4,5,6,7,8,9,1
2,6,7,8,9,1,2,3,4
8,9,1,2,3,4,5,6,7
3,4,5,6,7,8,9,1,2
6,7,8,9,1,2,3,4,5
9,1,2,4,3,5,6,7,8
当我在 IntelliJ IDEA Ultimate Edition 中手动 运行 JUnit 文件时,我的所有测试 运行 正确...
当我 运行 从命令行使用以下命令 mvn clean test
或 mvn test
时,我的单元测试没有 运行!
mvn clean test
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.sudoku:sudoku:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 28, column 21
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] -------------------------< com.sudoku:sudoku >--------------------------
[INFO] Building sudoku 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ sudoku ---
[INFO] Deleting /home/pnwlover/sudoku/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ sudoku ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ sudoku ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /home/pnwlover/sudoku/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ sudoku ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ sudoku ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /home/pnwlover/sudoku/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ sudoku ---
[INFO] Surefire report directory: /home/pnwlover/sudoku/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.sudoku.SudokuTest
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.563 s
[INFO] Finished at: 2021-07-17T12:43:13-07:00
[INFO] -------------------------------------------------------
根本原因可能是您的 Maven 版本。 Maven 3.6.0 于 2018 年 10 月 24 日发布。此版本包括 Maven Surefire Plugin 版本 2.22.0(单元测试 运行ner)和 Maven Failsafe 版本 2.22.0(集成测试 运行内尔)插件。 2.22.0 版本包括对 JUnit 的支持。
在这些版本之前,为了 运行 Maven 下的 JUnit 5 测试,您需要为 Maven Surefire 插件包含一个 JUnit 提供程序依赖项。
您将看到 Maven 的示例配置,如下所示:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
</plugin>
IntelliJ 可以 运行 它是自己的 maven 版本,可能不需要该插件定义
感谢您的快速回复,虽然@Krzysztof K 的解决方案有效,但我通过执行以下操作进行了修复:
- 升级了我的 Maven 版本并将
M2_HOME
更新为 Maven 3.8.1
- 将其插入我的 pom.xml 的插件部分:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
使用 Java 1.8 和 JUnit 1.5,我创建了一个数独求解器,它从我基于 maven 的项目的资源目录中读取有效和无效文件。问题是,当我 运行 在 Intellij IDEA Ultimate Edition 中进行 JUnit 测试时,单元测试 运行 并且一切正常!但是,当我从命令行 运行 使用以下命令时 mvn clean test
或 mvn test
,none 的测试 运行!
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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.sudoku</groupId>
<artifactId>sudoku</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
项目结构:
sudoku
│
├── sudoku.iml
├── src
│ ├── test
│ │ ├── resources
│ │ │ ├─ valid.csv
│ │ └──── invalid.csv
│ │ └── java
│ │ └── com
│ │ └── sudoku
│ │ └── SudokuTest.java
│ └── main
│ ├── resources
│ │ ├── valid.csv
│ │ └── invalid.csv
│ └── java
│ └── com
│ └── sudoku
│ └── Sudoku.java
├── pom.xml
Sudoku.java:
package com.sudoku;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Arrays;
public class Sudoku {
public static final int SIZE = 9;
private final int[][] board = new int[SIZE][SIZE];
public boolean isValidRow() {
for (int x = 0; x < SIZE; x++) {
if (!isValidBoard(board[x])) {
System.out.println("Invalid row: " + Arrays.toString(board[x]));
return false;
}
}
return true;
}
public static boolean isValidBoard(int[] boardMatrix) {
return Arrays.stream(boardMatrix).sum() == 45;
}
public void loadCsvFile(String csvFile) throws Exception {
URL resource = getClass().getClassLoader().getResource(csvFile);
BufferedReader csvReader = new BufferedReader(new InputStreamReader(resource.openStream()));
int x = 0;
String row = null;
while ((row = csvReader.readLine()) != null) {
String[] rows = row.split(",");
int y = 0;
for (String singleRow : rows) {
if (singleRow != null && !"".equals(singleRow)) {
int rowIntegerValue = 0;
rowIntegerValue = Integer.parseInt(singleRow);
if (rowIntegerValue < 1 && rowIntegerValue > 9) {
throw new IllegalArgumentException(rowIntegerValue + " is invalid. Must be in between 1 - 9.");
}
board[x][y] = rowIntegerValue;
y++;
}
}
x++;
}
csvReader.close();
}
}
SudokuTest.java
package com.sudoku;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class SudokuTest {
@Test
public void isValidRow() throws Exception {
Sudoku sudoku = new Sudoku();
sudoku.loadCsvFile("valid.csv");
assertTrue(sudoku.isValidRow());
}
}
valid.csv:
9,2,3,4,5,6,7,8,1
4,5,6,7,8,9,1,2,3
7,8,9,1,2,3,4,5,6
5,3,4,5,6,7,8,9,1
2,6,7,8,9,1,2,3,4
8,9,1,2,3,4,5,6,7
3,4,5,6,7,8,9,1,2
6,7,8,9,1,2,3,4,5
9,1,2,4,3,5,6,7,8
当我在 IntelliJ IDEA Ultimate Edition 中手动 运行 JUnit 文件时,我的所有测试 运行 正确...
当我 运行 从命令行使用以下命令 mvn clean test
或 mvn test
时,我的单元测试没有 运行!
mvn clean test
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.sudoku:sudoku:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 28, column 21
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] -------------------------< com.sudoku:sudoku >--------------------------
[INFO] Building sudoku 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ sudoku ---
[INFO] Deleting /home/pnwlover/sudoku/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ sudoku ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ sudoku ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /home/pnwlover/sudoku/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ sudoku ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ sudoku ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /home/pnwlover/sudoku/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ sudoku ---
[INFO] Surefire report directory: /home/pnwlover/sudoku/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.sudoku.SudokuTest
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.563 s
[INFO] Finished at: 2021-07-17T12:43:13-07:00
[INFO] -------------------------------------------------------
根本原因可能是您的 Maven 版本。 Maven 3.6.0 于 2018 年 10 月 24 日发布。此版本包括 Maven Surefire Plugin 版本 2.22.0(单元测试 运行ner)和 Maven Failsafe 版本 2.22.0(集成测试 运行内尔)插件。 2.22.0 版本包括对 JUnit 的支持。
在这些版本之前,为了 运行 Maven 下的 JUnit 5 测试,您需要为 Maven Surefire 插件包含一个 JUnit 提供程序依赖项。
您将看到 Maven 的示例配置,如下所示:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
</plugin>
IntelliJ 可以 运行 它是自己的 maven 版本,可能不需要该插件定义
感谢您的快速回复,虽然@Krzysztof K 的解决方案有效,但我通过执行以下操作进行了修复:
- 升级了我的 Maven 版本并将
M2_HOME
更新为 Maven 3.8.1 - 将其插入我的 pom.xml 的插件部分:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>