JUnit RunWith Example
In this example we are going to explain how to run simultaneously more than one test cases using JUnit
testing framework and Eclipse IDE. This collection of different test cases that will be executed all together is otherwise called JUnit test suite and for this purpose we use the @RunWith
and @Suite
annotations.
1. Create the java class to be tested
Open Eclipse IDE and create a new project called JunitRunWithExample
. Create a new package called com.javacodegeeks.junit.suite
and then, create a java class named FirstDayAtSchool.java
, which will be the class to be tested. Below is the code of the class FirstDayAtSchool.java
.
FirstDayAtSchool.java
package com.javacodegeeks.junit.suite; 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; } }
As we can observe in the above class, there are two methods, prepareMyBag()
and addPencils()
, that they are just printing out the content of an array of strings.
2. Create JUnit tests
In this section, we will create the test cases for the above class. Before you create the test class, it is considered as good practice to create a new source folder dedicated to tests. For this purpose, right-click your project, select New -> Source Folder, name the new source folder test
and click Finish.
You can also have the same result by right-clicking on your project and select Properties -> Java Build Path -> Source and Add Folder. You can see the result in the figure below:
Now we are ready to proceed with the creation of the JUnit test classes.
Right-click on the class that we want to test, in this case the class named FirstDayAtSchool.java
and select New → JUnit Test Case.
In the following wizard select the New JUnit 4 test flag and select the test source folder, so that your test class will be created in this folder.
If your project does not contain the JUnit library in its classpath, the following message will be displayed so as to add the JUnit library to the classpath.
Also, we would like to test only the first method of this class in this specific test, so we can select Next in the wizard and choose the method prepareMyBag()
.
The code of the first test class which is named FirstDayAtSchoolTest.java
, follows.
FirstDayAtSchoolTest.java
package com.javacodegeeks.junit.suite; import static org.junit.Assert.*; import org.junit.Test; public class FirstDayAtSchoolTest { FirstDayAtSchool school = new FirstDayAtSchool(); String[] bag = { "Books", "Notebooks", "Pens" }; @Test public void testPrepareMyBag() { System.out.println("Inside testPrepareMyBag()"); assertArrayEquals(bag, school.prepareMyBag()); } }
Following the same procedure, we create another test case for the class FirstDayAtSchool.java
which is called FirstDayAtSchoolTest2.java
and tests the second method of the class named addPencils()
.
FirstDayAtSchoolTest2.java
package com.javacodegeeks.junit.suite; import static org.junit.Assert.*; import org.junit.Test; public class FirstDayAtSchoolTest2 { FirstDayAtSchool school = new FirstDayAtSchool(); String[] bag = { "Books", "Notebooks", "Pens", "Pencils" }; @Test public void testAddPencils() { System.out.println("Inside testAddPencils()"); assertArrayEquals(bag, school.addPencils()); } }
3. Create test suite using annotation @RunWith
Now, we are going to create the test suite class. Right-click on the test source folder and select New -> Other -> JUnit -> JUnit Test Suite. Then, choose the name of the suite and the test classes that will be included in the suite and click Finish.
The class following is the test suite of the two test classes.
AllTests.java
package com.javacodegeeks.junit.suite; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) @SuiteClasses({ FirstDayAtSchoolTest.class, FirstDayAtSchoolTest2.class }) public class AllTests { }
In the test suite, we can see that there are two annotations, @RunWith
and @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.
@SuiteClasses
The @SuiteClasses
annotation specifies the classes to be executed when a class annotated with @RunWith(Suite.class)
is run.
Now we can run the test suite by right-clicking on the suite and select Run As -> JUnit Test.
If we run the above code, we will have the following results:
- Output:
Inside testPrepareMyBag() My school bag contains: [Books, Notebooks, Pens] Inside testAddPencils() Now my school bag contains: [Books, Notebooks, Pens, Pencils]
Download the source code
This was an example of how to execute test suites in JUnit testing framework along with the @RunWith
annotation.
You can download the full source code of this example here : JunitRunWithExample
how can i read the class name dynamically inside @SuiteClasses instead of hardcoding the value. example system.property or java variable