JUnit – Timeout Test

If a test case takes more time than specified number of milliseconds then Junit will automatically mark it as failed. Following example demonstrates Timeout Test using JUnit.

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml


1. JUnit Class

Create following class

package com.kruders.junit;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class JUnitTimeoutTest {
	public void testAdd(){
		assertEquals(15, 10 + 5);
		System.out.println("@Test called - Addition");
	@Test(timeout = 100)
	public void method(){

2. Testing
Now when you run the project, following screen will be displayed. 2nd method will not return so JUnit engine will mark it as failed and throw an exception.

Figure 2.1 Figure 2.1

No comments yet.

Leave a Reply