Skip to content

Instantly share code, notes, and snippets.

@wkorando
Created June 25, 2019 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wkorando/01cdf8bc9f2485878469730fb6e85d58 to your computer and use it in GitHub Desktop.
Save wkorando/01cdf8bc9f2485878469730fb6e85d58 to your computer and use it in GitHub Desktop.
@Timeout(3) // Sets timeout limit for each test in class to five 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