Examples
We can work with some examples to understand the implementation of the @Ignore Annotation better.
Ignore a Test Method
We will create a message java class to test for our first example. The class takes a message, then prints the message and a "Hi!" in front of the message. The java code looks like this:
Program
//MessageUtil.java
package ignore_annotation;
public class MessageUtil {
private String message;
//Constructor
//@param message to be printed
public MessageUtil(String message){
this.message = message;
}
// prints the message
public String printMessage(){
System.out.println(message);
return message;
}
// add "Hi!" to the message
public String salutationMessage(){
message = "Hi!" + message;
System.out.println(message);
return message;
}
}

You can also try this code with Online Java Compiler
Run Code
Now we can create a JUnit 4 test case for our class. We will ignore one of the two tests using the @Ignore Annotation in the test case.
Program
//TestJunit.java
package ignore_annotation;
import org.junit.Test;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;
public class TestJunit {
String message = "Ninja Coder";
MessageUtil messageUtil = new MessageUtil(message);
@Ignore
@Test
public void testPrintMessage() {
System.out.println("Inside testPrintMessage()");
message = "Ninja Coder";
assertEquals(message,messageUtil.printMessage());
}
@Test
public void testSalutationMessage() {
System.out.println("Inside testSalutationMessage()");
message = "Hi!" + "Ninja Coder";
assertEquals(message,messageUtil.salutationMessage());
}
}

You can also try this code with Online Java Compiler
Run Code
Output

We can see that only one of the two tests, i.e., testSalutationMessage, runs while ignoring testPrintMessage.
Ignore All The Tests of a Class
In our following example, we can update the test code from the first example to ignore all the class tests. To do that, we can remove @Ignore from the test and add it at the class level like this:
Program
//TestJunit.java
package ignore_annotation;
import org.junit.Test;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;
@Ignore
public class TestJunit {
String message = "Ninja Coder";
MessageUtil messageUtil = new MessageUtil(message);
@Test
public void testPrintMessage() {
System.out.println("Inside testPrintMessage()");
message = "Ninja Coder";
assertEquals(message,messageUtil.printMessage());
}
@Test
public void testSalutationMessage() {
System.out.println("Inside testSalutationMessage()");
message = "Hi!" + "Ninja Coder";
assertEquals(message,messageUtil.salutationMessage());
}
}

You can also try this code with Online Java Compiler
Run Code
Output

We can see that we ignore both the tests (testSalutationMessage and testPrintMessage).
FAQs
-
What are annotations in JUnit?
JUnit Annotations are a unique form of syntactic meta-data that you can add to Java source code for better readability and structure. You can annotate Variables, parameters, packages, methods, and classes. Annotations were introduced in JUnit 4, making Java code more readable and straightforward than JUnit 3. For example, @Test, @Before, @After, @Ignore, etc.
-
How do you use @Ignore Annotation in JUnit 5?
The @Disabled Annotation in JUnit 5 works similarly to the @Ignore Annotation in JUnit 4. The only observable difference in the case of @Ignore and @Disabled is that JUnit 5 shows 3/3 test cases skipped considering that three test methods were ignored out of the total three test methods in the class when applied at the class level. Whereas JUnit 4 shows, 1/1 class skipped.
-
How can we conditionally run or ignore tests in JUnit 4?
This Assume class provides methods to support conditional test execution based on certain conditions. That includes methods like assumeThat(), which checks that the state satisfies the conditions of the matcher passed.Others Methods are assumeTrue(), assumeFalse(), assumeNotNull().
Key Takeaways
A @Ignore Annotation is a potent tool that makes a test code written using the JUnit framework more readable and easy to understand. The introduction of @Ignore in JUnit 4 was a significant differentiator from JUnit3.
Through this blog, we have understood the implementation of the @Ignore Annotation and worked with examples to better understand the usage.
If you are starting out with unit testing, be sure to explore the jest framework as well with our introductory articles like Getting Started with Jest and Writing Your First Unit Test with Jest.
But the learning never stops, and there is a lot more to learn. So head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more. Till then, Happy Learning!