This Tutorial Explains how to Ignore Test Cases in JUnit with examples. You will learn to use @Ignore in JUnit 4 & @Disabled Annotation in JUnit 5:
In the previous tutorial, we got to grasp what the API called Annotation is, what it does and also saw basic examples on how to use lifecycle annotations, the priorities they hold when a test case is executed.
Let’s try to cast light on the situations when we need not run or are not supposed to run all the test cases. We will learn to Ignore Test Cases in JUnit.
=> Check ALL JUnit Tutorials Here.
Table of Contents:
JUnit Ignore Test Cases
There could be certain test cases that are not be run because they may not be pertaining to certain code changes or the code for the test cases may be still under development, so we avoid running them.
In such cases, we might need to run a set of test cases by skipping a few others. So, what is it that the JUnit 4, as well as JUnit 5, provides us with so that we are able to run only a few test cases while ignoring or disabling or call it ‘skipping’ a few of the test cases?
Fortunately, we have @Ignore annotation for JUnit 4 to skip a test case whereas @Disabled annotation for JUnit 5 to do the same.
JUnit 4 – @Ignore Annotation
- The JUnit 4 @Ignore annotation could be applied for a test method, to skip its execution. In this case, you need to use @Ignore with the @Test annotation for a test method you wish to skip.
- The annotation could also be applied to the test class, to skip all the test cases under a class. In this case, you need to use @Ignore at the class level.
The code needs the package org.junit.Ignore to be imported for @Ignore to work. Let’s demonstrate how to skip a test method in a JUnit 4 test. We shall modify the JUnitProgram.java to skip the first testcase method.
The code snippet is:
@Ignore @Test public void test_JUnit1() { System.out.println("This is the testcase test_JUnit1() in this class"); } @Test public void test_JUnit2() { System.out.println("This is the testcase test_JUnit2() in this class"); } @Test public void test_JUnit3() { System.out.println("This is the testcase test_JUnit3() in this class"); }
On the execution of the class file, the test_JUnit1() is skipped during execution. Besides, the method annotated with @Ignore and all other test methods run as expected.
The resultant Run count shows 3/3 test cases and 1 testcase shows skipped. The run count showed 3/3 because even the skipped testcase attempted to execute.
The below screenshot of the console window proves the same.
@Ignore Annotation With A Reason Parameter
There is a variation to the @Ignore annotation too. The annotation takes in a single argument with a string value which is the reason for skipping the test.
Let’s demonstrate this variation of @Ignore annotation.
The code snippet is as follows:
@Ignore("the testcase is under development") @Test public void test_JUnit1() { System.out.println("This is the testcase test_JUnit1() in this class"); }
The console window shows the same resultant as it was without the reason passed to @Ignore annotation.
Now, let us see how all the tests belonging to a class could be disabled. We shall now update the @Ignore annotation at the class level for JUnitProgram.java
The code snippet is as shown below:
import org.junit.AfterClass; @Ignore("the testcase is under development") public class JUnitProgram { @BeforeClass public static void preClass() { System.out.println("This is the preClass() method that runs one time before the class"); } @Before public void setUp() { System.out.println("_______________________________________________________\n"); System.out.println("This is the setUp() method that runs before each testcase"); } @Test public void test_JUnit1() { System.out.println("This is the testcase test_JUnit1() in this class"); }
Post execution of the class file, the console shows nothing, and the Run count under the JUnit tab shows 1 class skipped out of 1 class.
Below is the screenshot of the console window:
JUnit 5 – @Disabled Annotation
@Disabled annotation in JUnit 5 works similarly as @Ignore annotation in JUnit 4.
- You may disable or skip execution for a test method or a group of tests by applying the annotation at the Test level.
- Or all the tests could be skipped by applying @Disabled annotation at the class level instead of applying it to the test method level.
Like @Ignore, a reason could also be passed for @Disabled for any developer or business analyst to know why was a specific testcase skipped. The parameter remains optional just like in the case of @Ignore.
(Note: We shall avoid demonstrating the @Disabled annotation through an actual code to avoid repetition as it follows the exact fashion the @Ignore follows in JUnit 4.)
The only difference that you shall observe in the case of @Ignore Vs @Disabled is that when the annotation is applied at the class level, post-execution of the JUnit class file, the Run count in the case of JUnit 4, shows 1/1 class skipped.
Hence a count of the class being skipped is provided whereas in the case of JUnit 5 shows 3/3 test cases are skipped considering that three test methods were skipped out of the total three test methods in the class.
Hence, on the visibility of the skipped test cases count, JUnit 5 does a slightly better job when compared to JUnit 4.
Conclusion
In this tutorial, we learned what are the situations when we might need to skip the execution of a few test cases. We also learned how to skip certain test cases both in JUnit 4 as well as JUnit 5.
=> Visit Here To See The JUnit Training Series For All.