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

新手上路,请多包涵

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() 注释的方法中得到“测试”消息 @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)

我能想到的非常明显且唯一的原因是未调用 init() 方法。 @BeforeEach 的文档说

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

我还尝试在 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() 方法没有被调用?

原文由 David 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2k
2 个回答

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

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

将以下内容添加到您的 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 发布,翻译遵循 CC BY-SA 3.0 许可协议

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

原文由 Michael 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏