我正在使用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());
}
}我收到以下信息:

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

我在哪里犯了个错误?
发布于 2019-09-18 10:08:21
这可能是你有一个非常老版本的JUnit木星在你的类路径。
里程碑版本5.0.0-M3 (2016年11月,发布说明)将返回类型添加到assertThrows中。您的类路径中的版本似乎比这更老。
确保您使用的是最新版本的JUnit。
发布于 2019-09-18 10:23:16
我找到了解决办法。原来我使用的是JUNIT Jupiter的旧版本。
在我的build.gradle文件中有这样的依赖关系:
dependencies {
testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.0-M2'
}当我把它们更新到最新版本
dependencies {
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.5.2'
}它果然起作用了。
https://stackoverflow.com/questions/57988290
复制相似问题