JUnit – Ignore a Test

In JUnit, to ignore a test, just add a @Ignore annotation before or after the @Test method.

P.S Tested with JUnit 4.12

IgnoreTest.java

package com.mkyong;

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

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

public class IgnoreTest {

    @Test
    public void testMath1() {
        assertThat(1 + 1, is(2));
    }

    @Ignore
    @Test
    public void testMath2() {
        assertThat(1 + 2, is(5));
    }

    @Ignore("some one please create a test for Math3!")
    @Test
    public void testMath3() {
        //...
    }

}

In the above example, both testMath2() and testMath3() will be ignored.

FAQS

1. To ignore a test, why not just comment the test methods or @Test annotation?
A : The test runner will not report the test. In IDE, the test runner will display the ignored tests with different icon or color, and highlight it, so that you know what tests are ignored.

2. Why make a test that doesn’t test?
A : For large project, many developers are handling different modules, the failed test may caused by other teams, you can add @Ignore on the test method to avoid the test to break the entire build process.

A : Or you want someone to help to create the test, like @Ignore ("help for this method!"), the optional parameter(String) will be displayed in the test runner.

References

  1. JUnit – Ignore JavaDoc

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
7 Comment threads
3 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
8 Comment authors
Smithc87mkyongBetlistaCoderManAC Recent comment authors
newest oldest most voted
AC
Guest
AC

Hey buddy, just came across your website. Appreciate your efforts. I will come back again to learn things here. Keep up the good work.

trackback
JUnit Tutorials | J2EE Web Development Tutorials

[…] Tutorial 3 – Ignore Test Ignore certain method for unit test. […]

Smithc87
Guest
Smithc87

I’ve been absent for a while, but now I remember why I used to love this website. Thanks, I’ll try and check back more frequently. How frequently you update your site? befeacdebgkgbabb

CoderMan
Guest
CoderMan

Sorry, but when I run java file on Junit test. It test the function even if I add @ignore annotation. so, what do u think problem is ?

mkyong
Guest
mkyong

@Ignore is added since v4.0, please upgrade your JUnit.

ClintJCL@gmail.com
Guest
ClintJCL@gmail.com

Forgive me, but it seems weird to have this? Why make a test that doesn’t test?

Betlista
Guest
Betlista

There can be several reason, for example when project members are all around the world and you need some help completing the test, you want to commit it to version system, but do not want to break build process for others…

mkyong
Guest
mkyong

Thanks @Betlista. Also refer to the FAQs above.

Ramakrishna Chandragiri
Guest
Ramakrishna Chandragiri

Wow very useful..

Satya
Guest
Satya

Thanks For You Article, Very Interesting I would often visit here.