一,演示項目相關信息
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)