Close

JUnit - @Ignore examples

[Last Updated: May 4, 2017]

We can temporarily disable a test or a group of tests by placing @Ignore annotation on methods individually or on a test class to ignore all tests in that class.

Followings are the quick examples to see how to do that.

Ignoring individual test methods

public class MyTest {

  @BeforeClass
  public static void beforeClass() {
      System.out.println("in MyTest1#beforeClass method");
  }

  @Ignore
  @Test
  public void testMethod1() {
      System.out.println("in MyTest1#testMethod1");
  }

  @Test
  public void testMethod2() {
      System.out.println("in MyTest1#testMethod2");
  }
}
mvn -q test -Dtest=MyTest

Output

d:\example-projects\junit\junit-ignore-example>mvn -q test -Dtest=MyTest

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.MyTest
in MyTest1#beforeClass method
in MyTest1#testMethod2
Tests run: 2, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.028 sec

Results :

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

Ignoring tests at class level

@Ignore
public class MyTest2 {

  @BeforeClass
  public static void beforeClass() {
      System.out.println("in MyTest2#beforeClass method");
  }

  @Test
  public void testMethod1() {
      System.out.println("in MyTest2#testMethod1");
  }

  @Test
  public void testMethod2() {
      System.out.println("in MyTest2#testMethod2");
  }
}
mvn -q test -Dtest=MyTest2

Output

d:\example-projects\junit\junit-ignore-example>mvn -q test -Dtest=MyTest2

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.MyTest2
Tests run: 1, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.005 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 1

Example Project

Dependencies and Technologies Used:

  • junit 4.12: JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck.
  • JDK 1.8
  • Maven 3.3.9

Junit Ignore Annoation Examples Select All Download
  • junit-ignore-example
    • src
      • test
        • java
          • com
            • logicbig
              • example
                • MyTest.java

    See Also