JUnit Ignore Test using @Ignore Annotation

Suppose we have some failing unit test because of a system reconfiguration or a change of client requirements or even a developer wrote some bad tests and he is currently on vacation. But we need to run the tests in order to commit some mission critical code. What do you do? You can try to fix the test or comment out the test code to proceed. But this clutters the code base and will not report the test to JUnit. Well, since JUnit 4.x the @Ignore annotation was introduced. You can annotate a class or a method with @Ignore("reason - why unit test failed"), with the result that this method will be skipped at the time of test execution. The best part is that at the end of the execution you get a detailed statistics of not only how many of your tests were successful or failed but also the number of the skipped tests.

Ignoring a Test Method

You can annotate a method with the @Ignore annotation. This will ignore only the annotated method. All the other methods in the class will run fine.

package com.memorynotfound.test;

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

public class IgnoringTestMethod {

    @Ignore("failing unit test")
    @Test
    public void ignoring(){
        System.out.println("JUnit Ignore test method");
    }

    @Test
    public void test(){
        System.out.println("JUnit test method");
    }
}

Output

Test 'com.memorynotfound.test.IgnoringTestMethod.ignoring' ignored (failing unit test)
JUnit test method

Ignoring All Test Methods in a Class

You can also annotate your class with the @Ignore annotation. This will explicitly ignore all your JUnit tests in the class.

package com.memorynotfound.test;

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

@Ignore("not implemented correctly")
public class IgnoringTestClass {

    @Test
    public void test(){
        System.out.println("JUnit Ignore test class");
    }
}

Output

Test '..IgnoringTestClass' ignored (not implemented correctly)

References

You may also like...