Issue
Using Java 1.8 and JUnit 1.5, I created a sudoku solver which reads valid and invalid files from my maven based project's resources directory. The issue is that when I run the JUnit test within Intellij IDEA Ultimate Edition, the unit tests run and everything works! But, when I run from the command line using the following commands mvn clean test
or mvn test
, none of the tests run!
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>
Project structure:
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
When I run the JUnit file manually in IntelliJ IDEA Ultimate Edition, all my tests run correctly...
When I run from the command line using the following commands mvn clean test
or mvn test
, my unit tests do not run!
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] -------------------------------------------------------
Solution
The root cause is likely your Maven version. Maven 3.6.0 was released on October 24th, 2018. This release includes version 2.22.0 of the Maven Surefire Plugin (unit test runner) and 2.22.0 of the Maven Failsafe (integration test runner) plugin. The 2.22.0 releases include support for JUnit.
Prior to these releases, to run JUnit 5 tests under Maven, you needed to include a JUnit provider dependency for the Maven Surefire plugin.
You will see example configurations for Maven like this:
<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 can run it's own maven version which probably doesn't require that plugin definition
Answered By - Krzysztof K
Answer Checked By - Willingham (JavaFixing Volunteer)