spring boot单元测试之十六:junit5:用@Timeout注解判断测试运行是否超时(spring boot 2.4.4)
作者:互联网
一,演示项目相关信息
1,项目地址:
https://github.com/liuhongdi/timeouttest
2,功能:用@Timeout注解判断测试运行是否超时
3,项目结构:如图:
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,java代码说明
1,service/HelloService.java
@Service public class HelloService { //同步方法 public String synchSayHello() { try { Thread.sleep(3000); return "this is sync"; } catch (InterruptedException e) { e.printStackTrace(); return "error"; } } }
2,service/HelloServiceTest.java
@SpringBootTest class HelloServiceTest { @Autowired private HelloService helloService; @Test @Timeout(value = 3500, unit = TimeUnit.MILLISECONDS) @DisplayName("测试3.5秒,不会超时") void synchSayHello() { String ret = helloService.synchSayHello(); System.out.println(ret); assertThat(ret, equalTo("this is sync")); } @Test @Timeout(2) @DisplayName("测试2秒,会超时") void synchSayHelloFail() { String ret = helloService.synchSayHello(); System.out.println(ret); } }
三,测试效果
四,查看spring boot的版本:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.4)
标签:__,spring,boot,测试运行,ret,Timeout,synchSayHello,___,com 来源: https://www.cnblogs.com/architectforest/p/14636816.html