junit

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 JUnitIgnore is 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 here.

C:\Users\konstantina\JUnitIgnore>javac -classpath "C:\Users\konstantina\Downloads\junit-4.11.jar";"C:\Users\konstantina\Downloads\hamcrest-core-1.3.jar"; FirstDayAtSchool.java JunitIgnoreTest1.java JunitIgnoreTest2.java JunitIgnoreTestRunner.java

As we mentioned in the previous step, in case your classes are located in a package, you have to ensure that you are in the correct directory, e.g. junit according to the previous example.

  • Now run the JunitIgnoreTestRunner
C:\Users\konstantina\JUnitIgnore>java -classpath "C:\Users\konstantina\Downloads\junit-4.11.jar";"C:\Users\konstantina\Downloads\hamcrest-core-1.3.jar"; JunitIgnoreTestRunner

Attention: As we mentioned in the previous steps, if your classes are located into a package, for example package com.javacodegeeks.core.junit, you should first move up directories so as to find the JUnitIgnore directory. Specifically, you should do the following:

C:\Users\konstantina\JUnitIgnore\com\javacodegeeks\core\junit>cd ..

C:\Users\konstantina\JUnitIgnore\com\javacodegeeks\core>cd ..

C:\Users\konstantina\JUnitIgnore\com\javacodegeeks>cd ..

C:\Users\konstantina\JUnitIgnore\com>cd ..

C:\Users\konstantina\JUnitIgnore>

Now that JUnitIgnore is your current directory, you can run the JunitIgnoreTestRunner.

C:\Users\konstantina\JUnitIgnore>java -classpath "C:\Users\konstantina\Downloads\junit-4.11.jar";"C:\Users\konstantina\Downloads\hamcrest-core-1.3.jar"; com.javacodegeeks.core.junit.JunitIgnoreTestRunner
  • Here is the output of the first test case:
Inside testPrepareMyBag()
My school bag contains: [Books, Notebooks, Pens]
All tests finished successfully...

As we see in the output, the only test method which is executed is the first one ( testPrepareMyBag()). The second one testAddPencils() is not executed, as it’s annotated with the @Ignore annotation. So, the result is exactly what we expected.

Now, we will run the second test case JunitIgnoreTest2.java, following the procedure described before.

  • First of all, we have to update the JunitIgnoreTestRunner.java so as to run the second test case.

JunitIgnoreTestRunner.java (updated)

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(JunitIgnoreTest2.class);
		for (Failure fail : result.getFailures()) {
			System.out.println(fail.toString());
		}
		if (result.wasSuccessful()) {
			System.out.println("All tests finished successfully...");
		}
	}
}
  • When JUnitIgnore is your current directory, compile again all the classes in the directory
C:\Users\konstantina\JUnitIgnore>javac -classpath "C:\Users\konstantina\Downloads\junit-4.11.jar";"C:\Users\konstantina\Downloads\hamcrest-core-1.3.jar"; FirstDayAtSchool.java JunitIgnoreTest1.java JunitIgnoreTest2.java JunitIgnoreTestRunner.java
  • Now run the JunitIgnoreTestRunner so as to verify the new results.
C:\Users\konstantina\JUnitIgnore>java -classpath "C:\Users\konstantina\Downloads\junit-4.11.jar";"C:\Users\konstantina\Downloads\hamcrest-core-1.3.jar"; JunitIgnoreTestRunner
  • Here is the new output:
All tests finished successfully...

As we see in the output, by adding @Ignore annotation at class level, both test methods are ignored, so no test case is tested.

Download the source code

This was an example of @Ignore annotation in JUnit testing framework.
Download the source code of this example : JUnitIgnore.zip

Konstantina Dimtsa

Konstantina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she is currently pursuing M.Sc studies in Advanced Information Systems at the same department. She is also working as a research associate for NKUA in the field of telecommunications. Her main interests lie in software engineering, web applications, databases and telecommunications.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button