I'm trying to transition one of my java projects into a maven
build. I'm having trouble with the testing portion and looking for pointers. This is my first maven project, so it is totally possible I'm missing something basic...
I have a set of tests similar to below that pass when run inside of my IDE (IntelliJ) but when I clean and run the maven tests, I get weird fails in the surefire-reports
. It appears that the @BeforeEach
portion of the test is not run under the maven structure, which would explain the "null" accusations below, but I'm not sure. I'm running Java 16 and my pom.xml
is included below along with the simple example, and failure report.
On the issue of the pom.xml... I'm kind of stabbing in the dark for the dependencies here for junit
and others. Is there a better/canonical way to build the dependencies in the pom.xml for the imports used in the code? Perhaps there is something in IntelliJ that I have not discovered yet, but I seem to web surf for each dependency.
test code
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.HashSet;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestKiller {
private HashSet<Integer> mySet;
@BeforeEach
public void setup(){
mySet = new HashSet<>();
}
@Test
public void testAdd(){
mySet.add(1);
assertEquals(mySet.size(), 1, "should have 1 element");
}
}
maven's surefire-report "claim" of failure...
-------------------------------------------------------------------------------
Test set: schedule.TestKiller
-------------------------------------------------------------------------------
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec <<< FAILURE!
schedule.TestKiller.testAdd() Time elapsed: 0.002 sec <<< FAILURE!
java.lang.NullPointerException: Cannot invoke "java.util.HashSet.add(Object)" because "this.mySet" is null
at schedule.TestKiller.testAdd(TestKiller.java:24)
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>groupId</groupId>
<artifactId>AirSked</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<excludes>MakeSimpleSked.java</excludes>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.4</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.0</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>