In this tutorial, you will learn-

What is JUnit @Ignore test annotation
Junit Test Example – Ignore
Creating a simple test class without ignoring a test
Ignore a test method using @Ignore annotation
Using @ ignore annotation with Condition
Ignore all test methods using @Ignore annotation.

What is JUnit @Ignore test annotation

The @Ignore test annotation is used to ignore particular tests or group of tests in order to skip the build failure. @Ignore annotation can be used in two scenarios as given below:

If you want to ignore a test method, use @Ignore along with @Test annotation. If you want to ignore all the tests of class, use @Ignore annotation at the class level.

You can provide the reason for disabling a test in the optional parameter provided by @Ignore annotation. It will help other developers working on the same piece of code, to understand “why a particular test is disabled?” When the issue of that particular test is fixed, you can simply enable it by removing @Ignore annotation.

Junit Test Example – Ignore

As discussed in above definition, you can use @Ignore annotation to ignore a test or group of the test. Let’s understand it using simple example and in below given scenarios:

Creating a simple test class without ignoring a test. Ignore a test method using @Ignore annotation. Ignore a test method using @Ignore annotation with proper reason. Ignore all test method using @Ignore annotation.

Creating a simple test class without ignoring a test

Let’s create a simple Java class which prints two types of message.

First method prints a simple message and The second method prints a “hi” message

JunitTestExample.java Let’s create a JUnit test class to test JUnitMessage.java.

First test, named “testJUnitMessage()” tests “printMessage()” method of above class. Similarly the second test, named “testJUnitHiMessage” tests “testJUnitHiMessage” of above class.

TestRunner.java Let’s create a test runner class to execute JunitTestExample.java Output:

Print statement on console: Junit Hi Message is printing Hi!Guru99 Junit Message is printing Guru99

Ignore a test method using @Ignore annotation

Let’s create ignore test to disable a test in above example. For this, you need to use @Ignore in the method, you want to skip. Let’s do it for testJUnitMessage() of JunitTestExample.java JunitTestExample.java Output: Let’s execute and verify the output of above example. Below output shows that one test is skipped (disabled), see as marked below:

Print statement on console: Junit Hi Message is printing Hi!Guru99

Using @ ignore annotation with Condition

Let’s take the example of how to ignore a test and define the reason for ignoring along with it. As discussed above, to provide a reason you have one optional parameter in @Ignore annotation where you can provide the reason statement. JunitTestExample.java Output: Same as above.

Ignore all test methods using @Ignore annotation.

As discussed above to ignore all the tests in class, you need to use @Ignore annotation at the class level. Let’s modify above example to understand how to ignore all the tests: Output :

Print statement on console: As both the tests skipped by using @Ignore at class level so no statement would be printed on the console.

Summary:

In this tutorial, you learned how to ignore a single test, group of test or all tests by using @Ignore annotation. @Ignore annotation can be used in two scenarios as given below:

If you want to ignore a test method, use @Ignore along with @Test annotation. If you want to ignore all the tests of class, use @Ignore annotation at the class level.

You also learned how to provide a statement to make understand other developer, why a particular test is disabled.