java - Assertions.assertThrows 返回 void,而它必须返回 Throwable | J单元5

标签 java assert junit5 assertion throwable

我正在使用JUnit 5库,我尝试使用assertThrows并获取抛出的异常,以便获取异常的消息。我有这样一段代码:

import org.junit.jupiter.api.Assertions;
//import org.junit.Assert;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;

public class Tests {
    private static void execute() throws Exception {
        ArrayList<String> filter = ListString.filter(new ArrayList<String>(Arrays.asList("1", "2", "3")), "4");
    }

    @Test
    public void listStringTest() {
        // This part doesn't work 
        Throwable throwable = Assertions.assertThrows(Exception.class, Tests::execute);
        assertEquals("Exception texnt", throwable.getMessage());
    }
}

我收到以下消息:

enter image description here

但是documentation说断言抛出返回T extends Throwable,而不是void:

enter image description here

我哪里出错了?

最佳答案

您的类路径中可能有一个非常旧版本的 JUnit Jupiter。

里程碑版本 5.0.0-M3(2016 年 11 月,release notes)向 assertThrows 添加了返回类型。您的类路径中的版本似乎比该版本更旧。

确保您使用最新版本的 JUnit。

关于java - Assertions.assertThrows 返回 void,而它必须返回 Throwable | J单元5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57988290/

相关文章:

spring - 接口(interface)上的 @SpringBootTest 注释不起作用

java 聊天应用程序无法在其他机器上运行

java - 需要加速 GOval 对象的帮助(制作 Breakout 游戏)

java - 检查整数是否是键盘上的键

c - 使用断言处理错误检查

json - 在Go中为map[string]interface{}创建方法

java - JUnit 5 测试未运行/检测到

java - Mockito 使用 JUnit 5 注入(inject)模拟两次

java - 如何将 JTree 中的选择限制为特定类型的节点?

c - CMAKE_BUILD_TYPE=Release 是否暗示 -DNDEBUG?