相关文章推荐
坏坏的西瓜  ·  【Springboot】复杂单元测试启动类- ...·  1 年前    · 
狂野的泡面  ·  大容量导入和导出数据 (SQL ...·  1 年前    · 
热心的黄花菜  ·  【分享】宜搭集成自动化,Groovy节点一般 ...·  2 年前    · 
买醉的草稿本  ·  AWS系统盘缩容 - diameter - 博客园·  2 年前    · 
Code  ›  java - JUnit 5 不执行用 BeforeEach 注释的方法 -
科技新闻 jupiter junit
https://segmentfault.com/q/1010000042903332
骑白马的包子
2 年前
segmentfault segmentfault
注册登录
问答 博客 标签 活动
发现
✓ 使用“Bing”搜本站 使用“Google”搜本站 使用“百度”搜本站 站内搜索
注册登录
  1. 首页
  2. 问答
  3. Stack Overflow 翻译
  4. 问答详情

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

社区维基
1
发布于
2022-11-25
新手上路,请多包涵

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 许可协议

Stack Overflow 翻译 java maven junit5
阅读 395
2 个回答
得票 最新
社区维基
1
发布于
2022-11-25
✓ 已被采纳

您的 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 许可协议

社区维基
1
发布于
2022-11-25

在我的例子中,问题是 @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
 
推荐文章
坏坏的西瓜  ·  【Springboot】复杂单元测试启动类-只测试OpenFeign以及只测试Spring Data JPA - 一杯半盏 - 博客园
1 年前
狂野的泡面  ·  大容量导入和导出数据 (SQL Server) - SQL Server | Microsoft Learn
1 年前
热心的黄花菜  ·  【分享】宜搭集成自动化,Groovy节点一般用法,解析JSON数据-阿里云开发者社区
2 年前
买醉的草稿本  ·  AWS系统盘缩容 - diameter - 博客园
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号