JUnit Ignore Test Example
In this example we are going to see how to use @Ignore annotation in JUnit testing framework. Also, we will see how to run the created test cases from the command line by using the org.junit.runner.JUnitCore.
1. Create the java class to be tested
Create a folder named JUnitIgnore. This is the folder where your classes will be located. Using a text editor, create a Java class to be tested named FirstDayAtSchool.java. To make sure your file name is FirstDayAtSchool.java, (not FirstDayAtSchool.java.txt), first choose “Save as -> Save as type -> All files”, then type in the file name FirstDayAtSchool.java.
FirstDayAtSchool.java
import java.util.Arrays;
public class FirstDayAtSchool {
public String[] prepareMyBag() {
String[] schoolbag = {"Books", "Notebooks", "Pens"};
System.out.println("My school bag contains: "+Arrays.toString(schoolbag));
return schoolbag;
}
public String[] addPencils() {
String[] schoolbag = {"Books", "Notebooks", "Pens", "Pencils"};
System.out.println("Now my school bag contains: "+Arrays.toString(schoolbag));
return schoolbag;
}
}2. Create JUnit test cases
In the same directory (JUnitIgnore), use a text editor and create a java class named JunitIgnoreTest1.java which will be our first test case. Below is the code of this class.
JunitIgnoreTest1.java
import static org.junit.Assert.assertArrayEquals;
import org.junit.Ignore;
import org.junit.Test;
public class JunitIgnoreTest1 {
FirstDayAtSchool school = new FirstDayAtSchool();
String[] bag1 = {"Books", "Notebooks", "Pens"};
String[] bag2 = {"Books", "Notebooks", "Pens", "Pencils"};
@Test
public void testPrepareMyBag() {
System.out.println("Inside testPrepareMyBag()");
assertArrayEquals(bag1, school.prepareMyBag());
}
@Ignore
@Test
public void testAddPencils() {
System.out.println("Inside testAddPencils()");
assertArrayEquals(bag2, school.addPencils());
}
}Now, create another java class, named JunitIgnoreTest2.java which will be our second test case.
JunitIgnoreTest2.java
import static org.junit.Assert.assertArrayEquals;
import org.junit.Ignore;
import org.junit.Test;
@Ignore
public class JunitIgnoreTest2 {
FirstDayAtSchool school = new FirstDayAtSchool();
String[] bag1 = {"Books", "Notebooks", "Pens"};
String[] bag2 = {"Books", "Notebooks", "Pens", "Pencils"};
@Test
public void testPrepareMyBag() {
System.out.println("Inside testPrepareMyBag()");
assertArrayEquals(bag1, school.prepareMyBag());
}
@Test
public void testAddPencils() {
System.out.println("Inside testAddPencils()");
assertArrayEquals(bag2, school.addPencils());
}
}
We can see that both test cases include the @Ignore annotation. Below is a short explanation of this annotation.
@Ignore
The @Ignore annotation can be used when you want temporarily disable the execution of a specific test. Every method that is annotated with @Ignore won’t be executed.
In the JunitIgnoreTest1.java, the @Ignore annotates the second method testAddPencils() while in the JunitIgnoreTest2.java, the @Ignore annotates all the class. So, in the first case we expect that only the first method will be executed while in the second case, we expect that both test methods will be ignored. Execution of those test cases will prove if our assumptions are right.
Before moving to the next section, we would like to give a short explanation of the other two JUnit elements that we see in the code, @Test annotation and assertArrayEquals assertion.
@Test
The @Test annotation indicates that the public void method to which it is attached can be run as a test case.
void assertArrayEquals([String message], expectedArray, resultArray)
Asserts that the array expected and the resulted array are equal. The type of Array might be int, long, short, char, byte or java.lang.Object.
For further details related to JUnit Assertions and Annotations, you can have a look at JUnit using Assertions and Annotations Example.
3. Run your test cases from the command line
You can run your JUnit test outside Eclipse, by using the org.junit.runner.JUnitCore class. This class provides the runClasses() method which allows you to execute one or several test classes. The return type of runClasses() method is an object of the type org.junit.runner.Result. This object can be used to collect information about the tests. Also, in case there is a failed test, you can use the object org.junit.runner.notification.Failure which holds description of the failed tests.
The procedure below shows how to run your test outside Eclipse.
In the directory JUnitIgnore, use a text editor and create a new Java class named JunitIgnoreTestRunner.java with the following code.
JunitIgnoreTestRunner.java
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class JunitIgnoreTestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(JunitIgnoreTest1.class);
for (Failure fail : result.getFailures()) {
System.out.println(fail.toString());
}
if (result.wasSuccessful()) {
System.out.println("All tests finished successfully...");
}
}
}At first place, we will run only JunitIgnoreTest1.java, so the argument of runClasses method will be the first test case class.
- Open command prompt and move down directories so as to find the directory where your java classes are located:
C:\Users\konstantina>cd JUnitIgnore
Attention: If your classes are located inside a package, for example package com.javacodegeeks.core.junit, the structure of your classes should look like this:
C:\Users\
|
---> konstantina\
|
---> JUnitIgnore\
|
---> com\
|
---> javacodegeeks\
|
---> core\
|
---> junit\
|
---> FirstDayAtSchool.java
---> JunitIgnoreTest1.java
---> JunitIgnoreTest2.java
---> JunitIgnoreTestRunner.java
Thus, you should do the following so as to find the suitable directory for the compilation.
C:\Users\konstantina>cd JUnitIgnore C:\Users\konstantina\JUnitIgnore>cd com C:\Users\konstantina\JUnitIgnore\com>cd javacodegeeks C:\Users\konstantina\JUnitIgnore\com\javacodegeeks>cd core C:\Users\konstantina\JUnitIgnore\com\javacodegeeks\core>cd junit C:\Users\konstantina\JUnitIgnore\com\javacodegeeks\core\junit>
- When
JUnitIgnoreis your current directory, compile all the classes in the direcory
Attention: To run your JUnit tests outside Eclipse properly you need to add the needed JUnit library jars to the classpath of your program. You can find those library jars Download NOW!

