Table of contents
1.
Introduction
2.
Test Suits
2.1.
@Runwith and @SuiteClasses
3.
Example Test Suit
3.1.
Test Class1
3.2.
Test Class2
3.3.
Test Suite Class
3.4.
Output
3.4.1.
JUnit output
3.4.2.
Console output
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

JUnit Test Suites

Author Aditya Anand
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Welcome readers! In this blog, we will learn about test suites in JUnit. In large-scale projects, we have numerous test cases. Often we want to group these tests into groups so that we testers can run each group as per our requirements. Also, some tests may require a lot more time to run, or they might be using an API call to run. In these cases, we don’t want to run these tests every time we run our test cases. So using test suites, we separate these tests from other tests.

Let's get started, and I hope you learn a lot from this tutorial.

Test Suits

In JUnit, the test suite allows us to aggregate all test cases from multiple classes in one place and run it together.
JUnit test suites help to group and execute tests in bulk. Conducting tests separately for all test classes is not desired in most cases. Test suites help in achieving this grouping.

@Runwith and @SuiteClasses

To create and run the suite test, We need to annotate the test suite class with @RunWith annotation and provide the Suite.class in the parenthesis.

We also need to annotate with @SuiteClasses annotation. A list of all the test classes under this test suite is given inside the parenthesis.

Example Test Suit

Now let’s see the step-by-step process of creating and running a test suite. Before that, we will create two classes, namely testClass1 and testClass2, that we will group under the testSuite1 class.

Test Class1

Let’s create a JUnit test class named testClass1. It contains one @Test, which simply prints the class name in the console and implements an assertTrue.

package com.aditya04848.junit.helper;
import static org.junit.Assert.assertTrue;
import  org.junit.Test;
public class testClass1 {   
    @Test
    public void test() {
        System.out.println("test class 1");
        assertTrue(true);
    }
}
You can also try this code with Online Java Compiler
Run Code

Test Class2

Let’s create another JUnit test class called testClass2. It is similar to testClass1.

package com.aditya04848.junit.helper;
import static org.junit.Assert.assertTrue;
import  org.junit.Test;
public class testClass2 { 
    @Test
    public void test() {
        System.out.println("test class 2");
        assertTrue(true);
    }  
}
You can also try this code with Online Java Compiler
Run Code

Test Suite Class

Now let’s create a test suite named testSuite1 as a JUnit test. I will group the above two JUnit test classes in a suite.

We will annotate the testSuite1 class with @RunWith and @SuiteClasses annotation, and inside the parentheses, testClass1 and testClass2 are given. Inner implementation of testSuite1 class will be kept empty.

See the below code to understand better. 

package com.aditya04848.junit.helper;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({testClass1.class, testClass2.class})
public class testSuite1 {
}
You can also try this code with Online Java Compiler
Run Code

Output

JUnit output

We can see that running testSuite1 class, the two classes testClass1 and testClass2 under testSuite1 Class run successfully.

Console output

Check out JUnit Interview Questions here.

FAQs

  1. What is the @suite test In JUnit?
    The test suite bundles a few unit test cases and runs them together. In JUnit, both @RunWith and @Suite annotations run the suite tests.
     
  2. What is @selectclasses In the JUnit test?
    @SelectClasses @SelectClasses specifies the classes to select when running a test suite via @RunWith (JUnitPlatform.class). Pass ClassName.class as parameter to @SelectClasses annotation. Pass class names in the parameter as array (inside curly braces {}) to @SelectClasses annotation.
     
  3. What is @suitedisplayname annotation in JUnit?
    @SuiteDisplayName Use this annotation to give a display name for the annotated test class executed as a test suite on the JUnit Platform.
     
  4. Can we use spaces in the display name In the JUnit report?
    We can use spaces, special characters, and even emojis in the display name. By default, JUnit reporting prints the class name and method name in the IDE test report. We can use @DisplayName to specify a custom name that is easy to read and provide information about the test class and method.

Key Takeaways

In this article, we have extensively discussed the JUnit test suites topic; we have learned that it is vital to group test classes in different groups called test suites. We have learned to create a test suite class using @Runwith and @SuiteClasses.

We hope that this blog has helped you enhance your knowledge regarding JUnit Test Suits and if you would like to learn more, check out our articles on Time Test in JUnitJUnit Error Collector. Do upvote our blog to help other ninjas grow.

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Reading!

Live masterclass