@Ignore annotation in JUnit
Overview
In this post, we will learn how to ignore tests in JUnit. To ignore a test we use @Ignore annotation. @Ignore annotated method would not be executed and would be ignored by JUnit. The method annotated with ignore will be skipped.
Sample JUnit Test class
package com.testingdocs.junit; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import org.junit.Ignore; import org.junit.Test; public class IgnoreTestInJUnit { @Ignore @Test public void ignoreTestMethod() { System.out.println("This method will NOT run. "); } @Test public void executeTestMethod() { System.out.println("This method will run. Unless ignored at class level."); assertEquals(5,5); } }
In the above code the first method is annotated with @Ignore annotation. This method will be skipped.
Class level annotation
We can use the annotation at class level. If you use the annotation at the class level, all the tests in the class would be ignored during the test run. All the methods will be skipped.
package com.testingdocs.junit; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import org.junit.Ignore; import org.junit.Test; @Ignore public class IgnoreTestInJUnit { @Ignore @Test public void ignoreTestMethod() { System.out.println("This method will NOT run. "); } @Test public void executeTestMethod() { System.out.println("This method will run. Unless ignored at class level."); assertEquals(5,5); } }