gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:30:59 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com