Getting Started Testing with JUnit 5: Part 2 (Multiple Test)

Aziz Kale
Javarevisited
Published in
4 min readSep 25, 2023

I will continue discussing JUnit 5 in this article, which serves as a follow-up to the previous project. The project covered in this article is the same one mentioned in the previous piece. You can review or download the project via this link, and access the previous article through this link.

I will be using IntelliJ IDEA as my IDE.

Running Multiple Tests

JUnit simplifies the process of writing and running tests, including the ability to execute multiple tests at once. In this part of the JUnit article series, I will cover 3 different ways of multiple testing:

1) Test Execution Order: JUnit allows you to control the order in which tests are executed, providing flexibility for running specific tests first or last. You can use the @TestMethodOrder annotation to define the execution order.

I add the class ExampleTestMethodOrder in the path:

src/test/java/com/azizkale/junittestingproject/ExampleTestMethodOrder.java

package com.azizkale.junittestingproject;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;

public class ExampleTestMethodOrder {
@Test
@Order(2)
public void testSecond() {
System.out.println("second test completed!");
}

@Test
@Order(1)
public void testFirst() {
System.out.println("first test completed!");
}
}

As you see, the second test is above the first one. But I ordered them by the annotation @Order and the result:

2) Test Suites: You can create a test suite class that includes multiple test classes. By using the annotation@Suite, you can execute all the tests defined in those classes together. Test suites are helpful when you want to group related tests.

It is needed to add this dependency:

<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-engine</artifactId>
<version>1.8.1</version>
</dependency>

And the class at the path:

src/test/java/com/azizkale/junittestingproject/ExampleSuiteTest.java

package com.azizkale.junittestingproject;

import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasses({MathsTest.class, ExampleTestMethodOrder.class, SquareAreaCalculatingTest.class})
public class ExampleSuiteTest {
// This is an empty class, no additional code is needed.
}

As seen, it is an empty class. Because its role is to test all classes that are within the annotation @SelectClasses({...}). The result should be:

3) Package-Level Execution: Using the @SelectPackages annotation, you can specify an entire package to run all tests within that package. This approach is useful when you want to execute tests from various classes in a specific package. To provide an example of this concept, I am making a slight change to the package structure. This is because, to test a package, I need to create another package. In other words, a package cannot be tested within itself.

I have gathered all the test classes in the package1that I’ve created up to this point. In order to run all the tests within this package, I’ve introduced package2. Now, I’m adding the following code to the ExamplePackageLevelTest class in package2.

package com.azizkale.junittestingproject.package2;

import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectPackages("com.azizkale.junittestingproject.package1")
public class ExamplePackageLevelTest {
// This is an empty class, no additional code is needed.
}

As you see, I just added two annotation no test methods in this class. And when I run it, all tests in the package1 are executed and I get this result on the console:

As the name of the annotation @SelectPackagessuggests, you can test multiple packages as well. The only thing you should do is add multiple package names to the annotation @SelectPackages:

@Suite
@SelectPackages({
"com.azizkale.junittestingproject.package3",
"com.azizkale.junittestingproject.package1"})
public class ExamplePackageLevelTest {
// This is an empty class, no additional code is needed.
}

Conclusion

In this article, we have covered how to run multiple test functions within a class, how to execute multiple test functions located in various classes, and how to run multiple tests within a single package or across multiple packages.

You can access the project at this link.

--

--

Aziz Kale
Javarevisited

Highly Motivated, Passionate Full Stack Developer | EMM-IT Co. | Web: azizkale.com