关于SpringBoot项目中不使用@Test的expected

环境: 

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值