环境:
idea 2021.2.3(Ultimate Edition)
SpringBoot 2.7.1
jdk8
问题描述:
今天从github上拉下一个项目, 发现报错, 自己创建了一个SpringBoot项目研究, 是直接拷测试类的时候发现@Test(expected报红了,
解决:
expected属性: 用来指示期望抛出的异常类型.
找了一下原因, 发现该属性是Junit4版本的, Junit5中已经没有用这个了.
我用的SpringBoot 2.7.1, 是Junit5.(使用idea插件Maven Helper看的)
修改前:
修改后:
@Test(expected = CancellationException.class)
public void whenCancelingTheFuture_thenThrowsCancellationException() throws ExecutionException, InterruptedException {
Future<String> future = calculateAsyncWithCancellation();
future.get();
}
修改后:
@Test
public void whenCancelingTheFuture_thenThrowsCancellationException() throws ExecutionException, InterruptedException {
Assertions.assertThrows(CancellationException.class, () -> {
Future<String> future = calculateAsyncWithCancellation();
future.get();
});
}
使用Assertions.assertThrows代替了
参考文章:
https://www.baeldung.com/java-completablefuture
junit4 - Spring Boot 2 can't find @Test(expected = xxx) - Stack Overflow