Home Junit 5 Junit 5 Disable tests @Disabled examples

Junit 5 Disable tests @Disabled examples

In this article we will see Junit 5 disable tests or skip Junit 5 test cases by using @Disabled annotation at class level and method level with examples.

1. Why do we disable test cases?

During the development, we may want to disable or skip some test cases from execution because of there is know issue in the functionality or feature. To ignore a test in JUnit you can either comment a method we can remove @Test annotation above the class or method or we can comment @Test annotation, in that case disabled test case record wont be there in generated report or runner results. Its always good to include record for every test case in report whether its failed or skipped or passed and the reason.

In Junit 5 Jupiter we can disable tests using @Disabled annotation. We use @Ignore annotation to disable or ignore the tests in JUnit 4

2. Junit 5 disable tests using @Disabled

Junit 5 @Disabled accepts only one optional parameter, which indicates the reason this test is disabled. Its good practice that developers provide a short explanation for why a test class or test method has been disabled.

For example, @Disabled("Disabled until issue #319 has been resolved")

2.1. Disabling entire test class :

@Disabled("Disable until issue #319 got fixed")
public class Junit5DisableClassLevelTest {
	
	@Test
	void test_Add() {
		assertEquals(5, MathUtil.add(3, 2));
	}
	
	@Test
	void test_Multiply() {
		assertEquals(15, MathUtil.multiple(3, 5));
	}

	@Test
	void test_Devide() {
		assertEquals(5, MathUtil.devide(25, 5));
	}
	
	@Test
	void testIs_Prime() {
		assertTrue(MathUtil.isPrime(13));
	}
}

Runner Results

Junit5 disable tests results class level

2.2. Disabling test method :

Use @Disabled at method level annotation to disable particular test method.

public class Junit5DisableMethodLevelTest {
	
	@Test
	void test_Add() {
		assertEquals(5, MathUtil.add(3, 2));
	}
	
	@Test
	void test_Multiply() {
		assertEquals(15, MathUtil.multiple(3, 5));
	}

	@Test
	void test_Devide() {
		assertEquals(5, MathUtil.devide(25, 5));
	}
	
	@Disabled("Disable until issue #112 got fixed")
	@Test
	void testIs_Prime() {
		assertTrue(MathUtil.isPrime(13));
	}
}

Runner Results

Junit 5 disable particular test method

Conclusion :

In this article we have seen how to disabled test cases using Junit 5 @Disabled annotation at class level and method level with examples.

You also might be interested in following examples :

  1. Junit 5 Dynamic Tests
  2. Junit 5 tags and filter test cases
  3. Junit 5 Timeout
  4. Junit 5 Repeated tests
  5. Junit 5 parameterized test
  6. Spring boot Junit 5 test

Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories