Home » Core Java » junit » JUnit Suite Test Example

About Konstantina Dimtsa

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.

JUnit Suite Test Example

In this example we are going to explain how to use a test suite in JUnit testing framework. Test suite is a collection of some test cases from different classes that can be run all together using @RunWith and @Suite annotations. Also, we will see how to run these 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 JUnitSuite. This is the folder where your classes will be located. Using Notepad or another 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.

Want to be a JUnit Master ?

Subscribe to our newsletter and download the JUnit Programming Cookbook right now!

In order to help you master unit testing with JUnit, we have compiled a kick-ass guide with all the major JUnit features and use cases! Besides studying them online you may download the eBook in PDF format!



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 (JUnitSuite), use Notepad or another text editor and create two java classes that will be our test cases. The first class named JunitTest1.java has the following code.


import static org.junit.Assert.*;
import org.junit.Test;

public class JunitTest1 {

   FirstDayAtSchool school = new FirstDayAtSchool();
   String[] bag = {"Books", "Notebooks", "Pens"};

   public void testPrepareMyBag() {	
      System.out.println("Inside testPrepareMyBag()");    
      assertArrayEquals(bag, school.prepareMyBag());     

The second class named JunitTest2.java will be also used as a test case and has the following code.


import static org.junit.Assert.*;
import org.junit.Test;

public class JunitTest2 {

   FirstDayAtSchool school = new FirstDayAtSchool();
   String[] bag = {"Books", "Notebooks", "Pens", "Pencils"};

   public void testAddPencils() {	
      System.out.println("Inside testAddPencils()");    
      assertArrayEquals(bag, school.addPencils());     

In the above test cases we can see the annotation @Test and the assert assertArrayEquals. Let’s give a short explanation of those two elements.

  • @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. Create JUnit Test suite

In the same directory (JUnitSuite), use Notepad or another text editor and create a java class named JunitTestSuite.java. This class is the test suite of the two test cases in the previous section and has the following code.


import org.junit.runner.RunWith;
import org.junit.runners.Suite;
public class JunitTestSuite {   

In the test suite, we can see that there are two annotations, @RunWith and @Suite.SuiteClasses.
Let’s give a short explanation of them.

  • @RunWith

When a class is annotated with @RunWith, JUnit will invoke the class in which is annotated so as to run the tests, instead of using the runner built into JUnit.

  • @Suite.SuiteClasses

The SuiteClasses annotation specifies the classes to be executed when a class annotated with @RunWith(Suite.class) is run.

4. Run your test 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 JUnitSuite, use Notepad or another editor and create a new Java class named JunitTestSuiteRunner.java with the following code.


import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class JunitTestSuiteRunner {

	public static void main(String[] args) {

		Result result = JUnitCore.runClasses(JunitTestSuite.class);
		for (Failure fail : result.getFailures()) {
		if (result.wasSuccessful()) {
			System.out.println("All tests finished successfully...");
  • Open command prompt and move down directories so as to find the directory where your java classes are located:
C:\Users\konstantina>cd JUnitSuite

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:

      ---> konstantina\  
            ---> JUnitSuite\
                   ---> com\
                         ---> javacodegeeks\
                               ---> core\
                                     ---> junit\
                                           ---> FirstDayAtSchool.java       
                                           ---> JunitTest1.java   
                                           ---> JunitTest2.java  
                                           ---> JunitTestSuite.java  
                                           ---> JunitTestSuiteRunner.java  

Thus, you should do the following so as to find the suitable directory for the compilation.

C:\Users\konstantina>cd JUnitSuite

C:\Users\konstantina\JUnitSuite>cd com

C:\Users\konstantina\JUnitSuite\com>cd javacodegeeks

C:\Users\konstantina\JUnitSuite\com\javacodegeeks>cd core

C:\Users\konstantina\JUnitSuite\com\javacodegeeks\core>cd junit

  • When JUnitSuite is your current directory, compile all the classes in the direcory (the class to be tested, the two Test classes, the Suite class and the Runner class.

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\JUnitSuite>javac -classpath "C:\Users\konstantina\Downloads\junit-4.11.jar";"C:\Users\konstantina\Downloads\hamcrest-core-1.3.jar"; FirstDayAtSchool.java JunitTest1.java JunitTest2.java JunitTestSuite.java JunitTestSuiteRunner.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 JunitTestSuiteRunner
C:\Users\konstantina\JUnitAssertions>java -classpath "C:\Users\konstantina\Downloads\junit-4.11.jar";"C:\Users\konstantina\Downloads\hamcrest-core-1.3.jar"; JunitTestSuiteRunner

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

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

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

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

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


Now that JUnitSuite is your current directory, you can run the JunitTestSuiteRunner.

C:\Users\konstantina\JUnitSuite>java -classpath "C:\Users\konstantina\Downloads\junit-4.11.jar";"C:\Users\konstantina\Downloads\hamcrest-core-1.3.jar"; com.javacodegeeks.core.junit.JunitTestSuiteRunner 
  • Here is the output:
Inside testPrepareMyBag()
My school bag contains: [Books, Notebooks, Pens]
Inside testAddPencils()
Now my school bag contains: [Books, Notebooks, Pens, Pencils]
All tests finished successfully...

Download the source code

This was an example of test suite in JUnit testing framework.
Download the source code of this example : JUnitSuite.zip

(No Ratings Yet) Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Leave a Reply

Be the First to Comment!

Notify of