课程: Complete Guide To Java Testing with JUnit 5 & Mockito

今天就学习课程吧!

今天就开通帐号,24,600 门业界名师课程任您挑!

Timeout

Timeout

- [Instructor] Introduced in Junit 5, the Timeout annotation is a great tool for controlling the execution time of tests. This annotation allows developers to specify a maximum time limit for the execution of a test method. If the test method exceeds this specified timeout duration, it is considered a failure. This prevents tests from hanging indefinitely, and facilitates timely feedback during test execution. Let's use it in a code example. With this sample code, we want to verify a given operation is completed within five seconds. We can do this by adding a timeout annotation, @Timeout5. In between the parentheses, we add the number of seconds for the desired timeout duration. Here, it's five seconds. Let's run the test. And it looks like the test fails. This happens because the method's execution actually takes six seconds. The thread sleeps for 6,000 milliseconds or six seconds. Let's change this to 4,000 milliseconds and see if the test passes. We'll run it again. With a shorter…

内容