maven JUnit 5不执行用BeforeEach注解的方法

xe55xuns  于 2023-04-29  发布在  Maven
关注(0)|答案(8)|浏览(333)

JUnit 5不会在用@BeforeEach注解注解的测试类中调用我的方法,我在其中初始化测试中需要的测试对象的一些字段。当试图在测试方法(用@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时的测试输出中,我从test0()方法中得到了“testing”消息,该方法用@Test注解注解,但“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用于表示注解方法应在当前测试类中的每个@Test、@RepeatedTest、@ParameterizedTest、@TestFactory和@TestTemplate方法之前执行。
我还尝试在eclipse中运行测试,它们总是通过,没有任何错误。
我用的是Maven 3。5.3.我将JUnit命名为Jupiter 5。1.0作为我的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/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()方法没有被调用?

8ftvxx2r

8ftvxx2r1#

为了让Maven使用@BeforeEach正确执行测试,您必须通过pom.xml正确配置项目
项目的pom.xml必须包含以下部分:

  1. dependencyManagementJunit BOM
  2. dependencyJunit Jupiter
  3. pluginMaven Surefire插件
    这里有正式的文档,这里有正式的项目示例。
    下面是示例项目的pom.xmllink
    下面是示例项目的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>
c8ib6hqw

c8ib6hqw2#

在我的例子中,问题是我在测试的子类中覆盖了一个用@BeforeEach注解的方法,所以超级方法没有被调用。

3pmvbmvn

3pmvbmvn3#

Sam Brannen的回答对我有效,但似乎它不适用于2。22.0版本的maven-surefire-plugin,除非您将junit-platform-surefire-provider升级到1。2.0。注意!

7uzetpgm

7uzetpgm4#

在我的例子中,问题是@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
ipakzgxi

ipakzgxi5#

您的init()方法没有被调用,因为您没有指示Maven Surefire使用JUnit Platform Surefire Provider。
因此,令人惊讶的是您的测试甚至没有使用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>
vyu0f0g1

vyu0f0g16#

现在,没有必要为插件添加提供程序。只需将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>
mwyxok5s

mwyxok5s7#

我在我的Gradle项目中遇到了同样的问题。注意到,@Test annotation使用了错误的包(org.junit.Test),并在使用正确的包(org.junit.jupiter.api.Test)后修复了该问题

wfveoks0

wfveoks08#

缺少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>

相关问题