java JUnit 5 不执行用 BeforeEach 注释的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49441049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 10:21:46  来源:igfitidea点击:

JUnit 5 does not execute method annotated with BeforeEach

javamavenjunit5

提问by David

JUnit 5 does not invoke my method in a test class that is annotated with the @BeforeEachannotation, 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.

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());
   }

}

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

在运行时的测试输出中,mvn clean test我从test0()@Test注释注释的方法中得到“测试”消息,但不打印“之前”消息。

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)

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

我能想到的非常明显且唯一的原因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.

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

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

我还尝试在 eclipse 中运行测试,并且它们总是通过而没有任何错误。

I am using maven 3.5.3. I declared JUnit Jupiter 5.1.0 as dependency in my pom.xml

我正在使用 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>

Why is my init()method not invoked?

为什么我的init()方法没有被调用?

回答by Michael

In my case the problem was that the @Testannotation was taken from wrong import. Originally it was imported from org.junit.Test. Once I have switched it to org.junit.jupiter.api.Testthe problem was resolved.

在我的情况下,问题是@Test注释是从错误的导入中获取的。最初它是从org.junit.Test. 一旦我切换到它,org.junit.jupiter.api.Test问题就解决了。

Wrong original code:

错误的原始代码:

import org.junit.Test;

@BeforeEach
...some code

@Test
...some code

Correct fixed code:

正确的固定代码:

import org.junit.jupiter.api.Test;

@BeforeEach
...some code

@Test
...some code

回答by Sam Brannen

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

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

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

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

Adding the following to your pom.xmlshould solve the problem.

将以下内容添加到您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>

回答by nikli

There is junit-jupiter-apidependency missing

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>

回答by KHanusova

Nowadays it is not necessary to add provider to plugin. Just add junit-jupiter-engine to your dependencies (as written in official documentation https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html).

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

回答by Eric Romrell

Sam Brannen's answer worked for me, but it seems that it doesn't work with the 2.22.0 version of maven-surefire-plugin unless you upgrade the junit-platform-surefire-provider to 1.2.0. Be aware!

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