Skip to content

Instantly share code, notes, and snippets.

@wkorando

wkorando/TimeoutTest.java

Last active Jul 3, 2019
Embed
What would you like to do?
@Timeout(3) // Sets timeout limit for each test in class to three seconds
@TestMethodOrder(OrderAnnotation.class)
public class TimeoutTest {
static int testCounter = 0;
@BeforeAll
@Timeout(2) // If this timeout is exceeded, all tests are failed
public static void classSetupWithTimeout() throws InterruptedException {
// ...complex setup code
}
@BeforeEach
@Timeout(2) // If timeout is exceeded current test is failed, but next test will be
// attempted
public void methodSetupWithTimeout() throws InterruptedException {
Thread.sleep(1500 * testCounter);
}
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = 500L) // Default unit is seconds, but other options available
@Order(1)
public void testTestCaseTimeout() throws InterruptedException {
Thread.sleep(600);
}
@Test
@Order(2)
public void testExceedsClassTimeLimit() throws InterruptedException {
Thread.sleep(3500);
}
@Test
@Order(3)
public void timeoutTest1() {
testCounter = testCounter + 1;
}
@Test
@Order(4) // Will fail due to timeout
public void timeoutTest2() {
testCounter = testCounter + 1;
}
@Test
@Order(5) // Will fail due to timeout, but still attempted
public void timeoutTest3() {
testCounter = testCounter + 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment