JUnit 5 不执行用 BeforeEach 注释的方法
JUnit 5 does not execute method annotated with BeforeEach
JUnit 5 不会在用 @BeforeEach
注释注释的测试 class 中调用我的方法,我在其中初始化测试中需要的测试对象的某些字段。当尝试在测试方法(用 @Test
注释的方法)中访问这些字段时,我显然得到了 NullpointerException。所以我在方法中添加了一些输出消息。
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class TestClass {
private String s;
public TestClass() {
}
@BeforeEach
public void init() {
System.out.println("before");
s = "not null";
}
@Test
public void test0() {
System.out.println("testing");
assertEquals("not null", s.toString());
}
}
在 运行 mvn clean test
时的测试输出中,我从使用 @Test
注释注释的 test0()
方法中得到 "testing" 消息,但是"before" 消息未打印。
Running de.dk.spielwiese.TestClass
!!!testing!!!
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0 sec <<< FAILURE!
de.dk.spielwiese.TestClass.test0() Time elapsed: 0 sec <<< FAILURE!
java.lang.NullPointerException
at de.dk.spielwiese.TestClass.test0(TestClass.java:24)
我能想到的非常明显且唯一的原因是未调用 init()
方法。 @BeforeEach
的文档说
@BeforeEach is used to signal that the annotated method should be
executed before each @Test, @RepeatedTest, @ParameterizedTest,
@TestFactory, and @TestTemplate method in the current test class.
我也尝试了 运行 eclipse 中的测试,它们总是通过而没有任何错误。
我正在使用 maven 3.5.3。
我在 pom.xml
中将 JUnit Jupiter 5.1.0 声明为依赖项
<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>de.dk</groupId>
<artifactId>spielwiese</artifactId>
<version>0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spielwiese</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<archive>
<manifest>
<mainClass>de.dk.spielwiese.Spielwiese</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<finalName>Spielwiese</finalName>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>de.dk</groupId>
<artifactId>util</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
为什么我的 init()
方法没有被调用?
您的 init()
方法未被调用,因为您尚未指示 Maven Surefire 使用 JUnit 平台 Surefire 提供程序。
因此,令人惊讶的是您的测试甚至 运行 都没有使用 JUnit。相反,它是 运行 Maven Surefire 对他们所谓的 POJO Tests.
的支持
将以下内容添加到您的 pom.xml
应该可以解决问题。
<build>
<plugins>
<plugin>
<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>
</dependencies>
</plugin>
</plugins>
</build>
Sam Brannen 的回答对我有用,但它似乎不适用于 2.22.0 版本的 maven-surefire-plugin,除非您将 junit-platform-surefire-provider 升级到 1.2.0。注意!
缺少 junit-jupiter-api
依赖项
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
现在没有必要在插件中添加提供程序。只需将 junit-jupiter-engine 添加到您的依赖项中(如官方文档中所写 https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html)。
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
在我的例子中,问题是 @Test
注释是从错误的导入中获取的。
它最初是从 org.junit.Test
导入的。
一旦我将它切换到 org.junit.jupiter.api.Test
,问题就解决了。
原码错误:
import org.junit.Test;
@BeforeEach
...some code
@Test
...some code
正确的固定代码:
import org.junit.jupiter.api.Test;
@BeforeEach
...some code
@Test
...some code
在我的例子中,问题是我在测试的子类中覆盖了一个用@BeforeEach 注释的方法,所以没有调用超级方法。
我的 gradle 项目遇到了同样的问题。
注意到,@Test 注释使用了错误的包 (org.junit.Test
) 并且在使用正确的包 (org.junit.jupiter.api.Test
)
后修复了问题
为了让 Maven 使用 @BeforeEach
正确执行测试,您必须通过 pom.xml
正确配置您的项目
您的项目 pom.xml
必须包含以下部分:
dependencyManagement
与 Junit BOM
dependency
与 Junit Jupiter
plugin
与 Maven Surefire 插件
这是正式记录的here and the official project examples are here。
这是示例项目的 link pom.xml
。
为方便起见,这里是示例项目的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.example</groupId>
<artifactId>junit5-jupiter-starter-maven</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.7.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
</project>
JUnit 5 不会在用 @BeforeEach
注释注释的测试 class 中调用我的方法,我在其中初始化测试中需要的测试对象的某些字段。当尝试在测试方法(用 @Test
注释的方法)中访问这些字段时,我显然得到了 NullpointerException。所以我在方法中添加了一些输出消息。
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class TestClass {
private String s;
public TestClass() {
}
@BeforeEach
public void init() {
System.out.println("before");
s = "not null";
}
@Test
public void test0() {
System.out.println("testing");
assertEquals("not null", s.toString());
}
}
在 运行 mvn clean test
时的测试输出中,我从使用 @Test
注释注释的 test0()
方法中得到 "testing" 消息,但是"before" 消息未打印。
Running de.dk.spielwiese.TestClass
!!!testing!!!
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0 sec <<< FAILURE!
de.dk.spielwiese.TestClass.test0() Time elapsed: 0 sec <<< FAILURE!
java.lang.NullPointerException
at de.dk.spielwiese.TestClass.test0(TestClass.java:24)
我能想到的非常明显且唯一的原因是未调用 init()
方法。 @BeforeEach
的文档说
@BeforeEach is used to signal that the annotated method should be executed before each @Test, @RepeatedTest, @ParameterizedTest, @TestFactory, and @TestTemplate method in the current test class.
我也尝试了 运行 eclipse 中的测试,它们总是通过而没有任何错误。
我正在使用 maven 3.5.3。 我在 pom.xml
中将 JUnit Jupiter 5.1.0 声明为依赖项<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>de.dk</groupId>
<artifactId>spielwiese</artifactId>
<version>0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spielwiese</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<archive>
<manifest>
<mainClass>de.dk.spielwiese.Spielwiese</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<finalName>Spielwiese</finalName>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>de.dk</groupId>
<artifactId>util</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
为什么我的 init()
方法没有被调用?
您的 init()
方法未被调用,因为您尚未指示 Maven Surefire 使用 JUnit 平台 Surefire 提供程序。
因此,令人惊讶的是您的测试甚至 运行 都没有使用 JUnit。相反,它是 运行 Maven Surefire 对他们所谓的 POJO Tests.
的支持将以下内容添加到您的 pom.xml
应该可以解决问题。
<build>
<plugins>
<plugin>
<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>
</dependencies>
</plugin>
</plugins>
</build>
Sam Brannen 的回答对我有用,但它似乎不适用于 2.22.0 版本的 maven-surefire-plugin,除非您将 junit-platform-surefire-provider 升级到 1.2.0。注意!
缺少 junit-jupiter-api
依赖项
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
现在没有必要在插件中添加提供程序。只需将 junit-jupiter-engine 添加到您的依赖项中(如官方文档中所写 https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html)。
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
在我的例子中,问题是 @Test
注释是从错误的导入中获取的。
它最初是从 org.junit.Test
导入的。
一旦我将它切换到 org.junit.jupiter.api.Test
,问题就解决了。
原码错误:
import org.junit.Test;
@BeforeEach
...some code
@Test
...some code
正确的固定代码:
import org.junit.jupiter.api.Test;
@BeforeEach
...some code
@Test
...some code
在我的例子中,问题是我在测试的子类中覆盖了一个用@BeforeEach 注释的方法,所以没有调用超级方法。
我的 gradle 项目遇到了同样的问题。
注意到,@Test 注释使用了错误的包 (org.junit.Test
) 并且在使用正确的包 (org.junit.jupiter.api.Test
)
为了让 Maven 使用 @BeforeEach
正确执行测试,您必须通过 pom.xml
您的项目 pom.xml
必须包含以下部分:
dependencyManagement
与 Junit BOMdependency
与 Junit Jupiterplugin
与 Maven Surefire 插件
这是正式记录的here and the official project examples are here。
这是示例项目的 link pom.xml
。
为方便起见,这里是示例项目的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.example</groupId>
<artifactId>junit5-jupiter-starter-maven</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.7.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
</project>