junit

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.

Create new source folder for junit tests.
Figure 1: Create new source folder for junit tests.

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:

Figure 2
Figure 2

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.

Figure 3: Create a new junit test class.
Figure 3: Create a new junit test class.

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.

Figure 4: Add JUnit library to the classpath.
Figure 4: Add 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().

Figure 4: Select methods for which test method stubs will be created.
Figure 5: Select methods for which test method stubs will be created.

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.

Figure 5: Create a test suite.
Figure 6: Create a test suite.

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.

Download
You can download the full source code of this example here : JunitRunWithExample

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.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Veerabhadra
Veerabhadra
5 years ago

how can i read the class name dynamically inside @SuiteClasses instead of hardcoding the value. example system.property or java variable

Back to top button