How to Ignore Tests in Junit 4

1. Overview

In this post, we will learn how to ignore or disable test methods or classes while running test cases.
To ignore a test in JUnit you can either comment a method or delete the @Test annotation, but the test runner will not report such a test. Alternatively, you can add the @Ignore annotation in front or after @Test

Test runners will report the number of ignored tests, along with the number of tests that ran and the number of tests that failed. Note that @Ignore takes an optional parameter (a String) if you want to record a reason why a test is being ignored.

Let's create a simple maven project to demonstrate usage of ignore tests in Junit 4.

1. Create a Simple Maven Project

Let's create a standard Maven web project structure.
mvn archetype:generate
-DgroupId=org.yourcompany.project
-DartifactId=application

2. Project Packaging Structure

  • src/main/java – Java Source code packages and classes
  • src/main/resources – NON-Java Resources, such as property files and Spring configuration
Test
  • src/test/java – Test Source code packages and classes
  • src/test/resources – NON-Java Resources, such as property files and Spring configuration
── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── javadevelopersguide
    │   │           └── junit
    │   ├── resources
    └── test
        ├── java
        │   └── com
        │       └── javadevelopersguide
        │           └── junit
        │               └── TestCaseIgnoreTest.java
        └── resources

3. Update JUnit dependencies in a pom.xml file

<dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.12</version>
 <scope>test</scope>
</dependency>
4. Create TestCaseIgnoreTest.java Class
package com.developersguide.junit;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.junit.Ignore;
import org.junit.Test;

public class TestCaseIgnoreTest {

    @Test
    public void test1() {
       assertThat(1 + 1, is(2));
    }
 
    @Ignore
    @Test
    public void test2() {
       assertThat(1 + 2, is(3));
    }

    @Test
    public void test3() {
       assertThat(1 + 3, is(4));
    }

    @Ignore("Test is ignored as a demonstration")
    @Test
    public void test4() {
       assertThat(1 + 4, is(5));
    }

    @Test
    public void test5() {
       assertThat(1 + 5, is(6));
    }
}

Output:

You can run tests from the maven command as shown below.

5. Run the test

Run the test using maven from the command line
mvn test

6. Output

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building junit-ignoring-tests-examples 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ junit-ignoring-tests-examples ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ junit-ignoring-tests-examples ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ junit-ignoring-tests-examples ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ junit-ignoring-tests-examples ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\Git_Work\junit-developers-guide\junit-ignoring-tests-examples\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ junit-ignoring-tests-examples ---
[INFO] Surefire report directory: E:\Git_Work\junit-developers-guide\junit-ignoring-tests-examples\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.developersguide.junit.TestCaseIgnoreTest
Tests run: 5, Failures: 0, Errors: 0, Skipped: 2, Time elapsed: 0.068 sec

Results :

Tests run: 5, Failures: 0, Errors: 0, Skipped: 2

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.302 s
[INFO] Finished at: 2018-06-27T22:26:31+05:30
[INFO] Final Memory: 12M/29M
[INFO] ------------------------------------------------------------------------

6. Conclusion

In this guide, we have learned the how to Ignore Tests in Junit by creating a maven project. The source code available for this guide on GitHub](https://github.com/RameshMF/junit-developer-guide/tree/master/junit-ignoring-tests-examples). On GitHub, there is a multi-module project and this getting started project is a module of it.
GitHub Repository: JUnit Developers Guide

Comments