簡體   English   中英

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 testmvn test ,沒有任何測試運行!


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


數獨.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();
    }
}

數獨測試程序

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());
    }
}

有效.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 testmvn 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 版和 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM