HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Junit / JUnit Test Suite Example

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 !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Vivek

    September 19, 2019

    How to run test suite with maven sure fire plugin?

  2. Fan

    July 29, 2015

    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.

    • Lokesh Gupta

      July 30, 2015

      Will this help : https://howtodoinjava.com/junit/how-to-add-listner-in-junit-testcases/

  3. Chindu

    August 7, 2014

    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?

    • Lokesh Gupta

      August 7, 2014

      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.

      • Chindu

        August 7, 2014

        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

        • Lokesh Gupta

          August 7, 2014

          Using annotations, you can’t do it.

          • Chindu

            August 7, 2014

            We can use Filter.shouldRun but that is a complex way of filtering
            I am looking for a simple way.
            Anyway thanks for replying Lokesh

            • Lokesh Gupta

              August 7, 2014

              Thanks for pointing out “Filter.shouldRun()”. It’s new concept for me. I will checkout it soon and share more on it.

  4. raga saleh

    July 30, 2014

    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

    • Lokesh Gupta

      July 30, 2014

      NO. it will not run the testcases in order. For maintaining the ordering, follow the information given here: https://howtodoinjava.com/junit/ordered-testcases-execution-in-junit-4/

  5. Micheal

    December 2, 2013

    Hi Lokesh, nice article but i have a doubt can we execute TestFeatureOne.class, TestFeatureTwo.class in specific order?

    • Lokesh Gupta

      December 2, 2013

      Not sure if it may solve you need. https://howtodoinjava.com/junit/ordered-testcases-execution-in-junit-4/
      Apart from this, testcases which needs certain ordering, are not considered good.
      Read more: https://howtodoinjava.com/best-practices/unit-testing-best-practices-junit-reference-guide/

Comments are closed on this article!

Search Tutorials

JUnit 5 Tutorial

  • JUnit 5 – Introduction
  • JUnit 5 – Test LifeCycle
  • JUnit 5 – @BeforeAll
  • JUnit 5 – @BeforeEach
  • JUnit 5 – @AfterEach
  • JUnit 5 – @AfterAll
  • JUnit 5 – @RepeatedTest
  • JUnit 5 – @Disabled
  • JUnit 5 – @Tag
  • JUnit 5 – Expected Exception
  • JUnit 5 – Assertions Examples
  • JUnit 5 – Assumptions
  • JUnit 5 – Test Suites
  • JUnit 5 – Gradle Dependency
  • JUnit 5 – Maven Dependency
  • JUnit 5 – Execute Test in Eclipse
  • JUnit 5 – Eclipse Test Templates
  • JUnit 5 vs JUnit 4

JUnit 4 Tutorial

  • JUnit – Introduction
  • JUnit – Test Suite
  • JUnit – Execute with JUnitCore
  • JUnit – Execute with Maven
  • JUnit – org.junit.Assume
  • JUnit – Expected Exceptions
  • JUnit – Listeners
  • JUnit – Force Timeout
  • JUnit – Ordered Tests
  • JUnit – Parameterized Tests
  • Junit – @Theory And @DataPoints
  • JUnit – TemporaryFolder @Rule

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)