Monday, January 10, 2011

JUnit Annotation Type - Ignore

2. Annotation Type Ignore:

Sometimes you want to temporarily disable a test or a group of tests. Methods annotated with Test that are also annotated with @Ignore will not be executed as tests. Also you can annotate a class containing test methods with @Ignore and none of the containing tests will be executed. Native JUnit 4 test runners should report the number of ignored tests along with the number of tests that ran and number of tests that failed.

For example:

@Ignore @Test public void something(){ ...

@Ignore takes an optional default parameter if you want to record why a test is being ignored.

@Ignore("not ready yet") @Test public void something() { ...

@Ignore can also be applied to the test class.
                 @Ignore public class IgnoreMe{
                             @Test public void test1(){....}
                             @Test public void test2(){....}
                  }

Example: Create a following class in Eclipse and name it as JUnitTest4

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

public class JUnitTest4
{
       @Test
       public void test1()
       {
              System.out.println("test1");
       }

       @Ignore("not ready yet")
       @Test
       public void test2()
       {
              System.out.println("test2");
       }

       @Test
       public void test3()
       {
              System.out.println("test3");
       }
}

Right click within the test method area and choose "Run As - JUnit Test"

Figure: Example for JUnit @Ignore annotation

No comments:

Post a Comment