TimeOut in Junit

We have a scenario where if our test case is taking more time to execute than that of the expected time then we can restrict using Junit to wait for specified amount of milliseconds and proceed further to rest of the test cases.

Let’s see how we are going to achieve this with an example :

Example 1 :


OUTPUT :

Junit-Timeout-Example1 Junit-Timeout-Example4

Junit will throw an “TestTimedOutException” error if the above test is not executed within 5 milliseconds and marks this test case as failed.

Let’s see how we are going to achieve this with an example :

Example 2:


OUTPUT :

Junit-Timeout-Example2

Junit-Timeout-Example3

Junit will execute both the statements as there is no TimedoutException occurs. Because two statements are executed within 3 seconds.

Leave a Comment

Your email address will not be published.