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
.
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 (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.
JunitTest1.java
import static org.junit.Assert.*; import org.junit.Test; public class JunitTest1 { FirstDayAtSchool school = new FirstDayAtSchool(); String[] bag = {"Books", "Notebooks", "Pens"}; @Test 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.
JunitTest2.java
import static org.junit.Assert.*; import org.junit.Test; public class JunitTest2 { FirstDayAtSchool school = new FirstDayAtSchool(); String[] bag = {"Books", "Notebooks", "Pens", "Pencils"}; @Test 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.
JunitTestSuite.java
import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ JunitTest1.class, JunitTest2.class }) 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.
JunitTestSuiteRunner.java
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()) { System.out.println(fail.toString()); } 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:
C:\Users\ | ---> 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 C:\Users\konstantina\JUnitSuite\com\javacodegeeks\core\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 .. C:\Users\konstantina\JUnitSuite>
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
hello.
when i create test class in path “src-test-java-com.package-JunitTestSuite.java”
and main class with method
public static void main(String[] args) {
in “src-main-java-com.package-Main.java”
it doesn’t work
why you doesn’t share normal project in ZIP file, with normal structure?
just like throw…in face…
i must put my test files in “src\main” ? with my Main.class ?