Junit 4 Test Suite Example in Java

In this example I am going to explain how to use a test suite in Junit testing framework in Java programming language. A test suite bundles a few unit test cases and runs them together. In Junit, both @RunWith and @Suite annotations are used to run the suite test.

Junit Classes : Junit classes are important classes which used in writing and testing Junit test cases. Some of the important classes are – Assert, TestCase, TestResult.

Test Runners : Test runners are used for executing test cases.

Prerequisites

Java at least 8, Junit 4.13.1

Project Setup

You can create a maven based project in your favorite IDE or tool. The name of the project is java-junit4-test-suit.

The following Junit dependency used in the pom.xml file. Later you can download the whole source code from the bottom of this tutorial.

<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.13.1</version>
	<scope>test</scope>
</dependency>

Junit Test Class

Now I am goin to create two test classes which will be used in test suite to run them together. These test classes are created under the test folder src/test/java.

package com.roytuts.java.junit4.test.suit;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class TestJunitOne {

	@Test
	public void testOne() {
		System.out.println("testOne() method in TestJunitOne");
		String str = "JunitOne is working fine";
		assertEquals("JunitOne is working fine", str);
	}

}

@Test: The Test annotation indicates that the public void method to which it is attached can be run as a test case.

assertEquals("JunitOne is working fine", str): it asserts that the expected and the resulted strings are equal.

package com.roytuts.java.junit4.test.suit;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class TestJunitTwo {

	@Test
	public void testTwo() {
		System.out.println("testTwo() method in TestJunitTwo");
		String str = "JunitTwo is working fine";
		assertEquals("JunitTwo is working fine", str);
	}

}

Test Suite Class

The test suite class includes the test classes which have to be run together. It uses @Suite.SuiteClasses annotation to include the test classes.

package com.roytuts.java.junit4.test.suit;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({ TestJunitOne.class, TestJunitTwo.class })
public class TestJunitSuite {

}

@RunWith: When a class is annotated with this annotation, Junit will invoke the class so as to run the tests, instead of using the runner built into Junit.

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

Test Runner 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 junit tests. Also, in case there is any failed test, you can use the object org.junit.runner.notification.Failure which holds description of the failed test.

package com.roytuts.java.junit4.test.suit;

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

public class TestRunner {

	public static void main(String[] args) {
		Result result = JUnitCore.runClasses(TestJunitSuite.class);

		for (Failure failure : result.getFailures()) {
			System.out.println(failure.toString());
		}

		System.out.println(result.wasSuccessful());
	}

}

Now execute the TestRunner class, you will see the below output in the console

testOne() method in TestJunitOne
testTwo() method in TestJunitTwo
true

Now if you make change the below line in TestJunitTwo class:

String str = "JunitTwo1 is working fine";

Now run the TestRunner class again, you will see that the class org.junit.runner.notification.Failure has given the details about failed test:

testOne() method in TestJunitOne
testTwo() method in TestJunitTwo
testTwo(com.roytuts.java.junit4.test.suit.TestJunitTwo): expected:<JunitTwo[] is working fine> but was:<JunitTwo[1] is working fine>
false

That’s all about test suite example in Junit.

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *