我正在使用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());
}
}
我收到以下消息:
但是documentation说断言抛出返回T extends Throwable
,而不是void
:
我哪里出错了?
最佳答案
您的类路径中可能有一个非常旧版本的 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/