JUnit 5不执行使用BeforeEach注释的方法 [英] JUnit 5 does not execute method annotated with BeforeEach

查看:413
本文介绍了JUnit 5不执行使用BeforeEach注释的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JUnit 5不会在使用 @BeforeEach 注释注释的测试类中调用我的方法,其中我初始化测试对象中需要的一些字段。试验。当试图在测试方法(使用 @Test 注释的方法)中访问这些字段时,我显然会得到NullpointerException。所以我在方法中添加了一些输出消息。

JUnit 5 does not invoke my method in a test class that is annotated with the @BeforeEach annotation, where I initialize some fields of the test object that are needed in the tests. When trying to access these fields inside a test method (method annotated with @Test) I obviously get a NullpointerException. So I added some output messages to the methods.

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 注释,但不打印之前消息。

In the output of the tests when running mvn clean test I get the "testing" message from the test0() method annotated with @Test annotation, but the "before" message is not printed.

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 的文档说

The very obvious and only reason that I can think of is that the init() method is not invoked. The documentation of @BeforeEach says


@BeforeEach用于表示带注释的信号在每个@Test,@ RepeatedTest,@ ParameterizedTest,
@TestFactory和当前测试类中的@TestTemplate方法之前执行
方法。

@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中运行测试,并且它们总是没有任何错误地通过。

I also tried running the tests in eclipse and there they always pass without any errors.

我正在使用maven 3.5.3。
我在我的pom.xml中声明了JUnit Jupiter 5.1.0作为依赖项

I am using maven 3.5.3. I declared JUnit Jupiter 5.1.0 as dependency in my 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()方法未被调用?

Why is my init() method not invoked?

推荐答案

您的 init ()方法未被调用,因为您没有指示Maven Surefire使用JUnit平台Surefire提供程序。

Your init() method is not invoked because you have not instructed Maven Surefire to use the JUnit Platform Surefire Provider.

因此,令人惊讶您的测试甚至没有使用JUnit运行。相反,它正在运行Maven Surefire支持他们所谓的 POJO测试

Thus, surprisingly your test is not even being run with JUnit. Instead, it is being run with Maven Surefire's support for what they call POJO Tests.

将以下内容添加到 pom.xml 应该可以解决问题。

Adding the following to your pom.xml should solve the problem.

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

这篇关于JUnit 5不执行使用BeforeEach注释的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆