JUnit Test Suite Example

JUnit test suites help to grouping and executing tests in bulk. Executing tests separately for all test classes is not desired in most cases. Test suites help in achieving this grouping.

In JUnit, test suites can be created and executed with these annotations.

  1. @RunWith
  2. @SuiteClasses

Read More : JUnit 5 Test Suite

1. JUnit test suite example

1.1. Test classes

Given below are JUnit test classes.

package com.howtodoinjava.junit;

import junit.framework.Assert;

import org.junit.Test;

public class TestFeatureOne {
	@Test
	public void testFirstFeature()
	{
		Assert.assertTrue(true);
	}
}
package com.howtodoinjava.junit;

import junit.framework.Assert;

import org.junit.Test;

public class TestFeatureTwo {
	@Test
	public void testSecondFeature()
	{
		Assert.assertTrue(true);
	}
}

1.2. Create junit test suite

To run above features only, we can write a suite like this.

package com.howtodoinjava.junit.suite;

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

import com.howtodoinjava.junit.TestFeatureOne;
import com.howtodoinjava.junit.TestFeatureTwo;

@RunWith(Suite.class)
@SuiteClasses({ TestFeatureOne.class, TestFeatureTwo.class })
public class TestFeatureSuite {
	//
}

1.3. Execute junit test suite

You can use JUnitCore to run test suite from application code.

Result result = JUnitCore.runClasses(testCase);

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

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

13 thoughts on “JUnit Test Suite Example”

  1. Nice setup, but what if I want to print out which Test Feature failed specifically. Using your solution, I get that TestFeatureSuite Failed and then it says which test case failed, for example @Test testSecondFeature failed, but it does not say that the testSecondFeature test case failed in TestSecondFeature class. What I am interested in is to know that TestSecondFeature class failed.

    Reply
  2. How can we add a condition before running the test cases?
    That is, I have added say two test cases in @SuiteClasses and I want to run one of them only if a certain condition is met?

    Reply
    • I do not find the concept of “run one of them only if” any good. Good written testcases should all be independent units. They should not be dependent on or reply on the result/output of any other testcase in application. Having dependent testcases mostly turnout to be maintenance nightmare in future.

      If you want something to assert before executing a testcase, then use asserts. They are present for this purpose only.

      Reply
      • In Junit 3 we can add the test cases conditionally like below:

        public class MyTestsuite extends TestCase
        {
        public static Test suite()
        {
        TestSuite suite = new TestSuite();
        if(some_condition_true)
        {
        suite.addTest(A.suite);
        }
        suite.addTest(B.suite);
        return suite;
        }
        }

        I wanted the same conditional adding using @SuitClasses

        Reply
  3. Hi

    is this mean that the tests will run in this order? (first test is TestFeatureOne and after it TestFeatureTwo)
    my goal is to build a suite that execute the tests in order should this help me?

    thanks

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.