Saturday 26 June 2021

Junit5: Disabled: Disable test methods or test methods in a class

Disabled annotation is used to signal that the annotated test class or test method is currently disabled and should not be executed.

 

When you apply @Disabled annotation at class level, all the test methods in that class get disabled automatically.

 

When you apply @Disabled annotation on a test method, only that method is disabled, remaining test methods in the test class gets executed.

 

DisableTestMethodsDemo.java

package com.sample.app;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

public class DisableTestMethodsDemo {

	@Test
	void helloTest1() {
		assertTrue(true);
	}
	
	@Test
	@Disabled
	void helloTest2() {
		assertTrue(true);
	}
	
	@Test
	void helloTest3() {
		assertTrue(true);
	}
}

In the above snippet, I disabled the test method ‘helloTest2’.

 

You can even specify the reason, why you disabled the test class and test method.

 

DisableTestClass.java

package com.sample.app;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

@Disabled(value="Tests are taking more time and not mandatory for this release")
public class DisableTestClass {

	@Test
	public void anonymmoustTest1() {
		assertTrue(true);
	}
	
	@Test
	public void anonymmoustTest2() {
		assertTrue(true);
	}
	
}


When you ran all test cases of your application, you can observe that the Disabled test methods/classes are skipped from execution.



You can download complete working application from this link.

https://github.com/harikrishna553/junit5/tree/master/junit5-examples

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment