Edit: I noticed that this is only a problem on JMockit 1.42 and later, so I opened an issue here
I have something similar to the following code
class Type1 {
}
class Type2 {
private final Type1 TYPE_1;
Type2(Type1 type1) {
TYPE_1 = type1;
}
// A bunch of methods involving TYPE_1
}
I am trying to test Type2
with TestNG by mocking a Type1
instance with JMockit. I have the following test.
class Type2Test {
@Tested
Type2 type2;
@Injectable
Type1 type1;
@Test
void randomTestThatShouldPass() {
Assert.assertTrue(true);
}
}
However, when running this test, I get a NullPointerException
with no apparent stack trace. The following is all that shown on my console.
java.lang.NullPointerException
I have tried a bunch of things to solve my problem including:
- Making TYPE_1 non-final
- Removing
@Injectable
fromtype1
inType2Test
. This gives me a better error message by telling me JMockit could not instantiatetype2
because there are no@Injectable
instances to invoke the constructor forType2
- Changed
@Injectable
to@Mocked
- Switching environments. The same problems occur on both VSCode and Intelij
- Using Gradle instead of Maven
The only thing that solves it is by removing all of JMockit's annotations, which is not helpful. What am I doing wrong in my test setup?
Edit: Responding to paulsm4 comment, I am not using Spring, but I am using Maven 3.5.3 with the following pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ok.ubc.cosc.VisualizeCode</groupId>
<artifactId>VisualizeCode</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>VisualizeCode</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>1.48</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>