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

JUnit Classes : JUnit classes are important classes, used in writing and testing JUnits. Some of the important classes are – Assert, TestCase, TestResult.

Test Runners : Test runner is used for executing test cases.

Example

Create dummy test classes under test folder src/test/java in Eclipse

package com.roytuts.junit.testsuit;

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) : Asserts that the expected and the resulted strings are equal.

package com.roytuts.junit.testsuit;

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);
    }

}

Create the Test Suite class

package com.roytuts.junit.testsuit;

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 @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.

Create 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.junit.testsuit;

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.junit.testsuit.TestJunitTwo): expected:<JunitTwo[] is working fine> but was:<JunitTwo[1] is working fine>
false

Thanks for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

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