0
0
JUnittesting~15 mins

Package-level test organization in JUnit - Build an Automation Script

Choose your learning style9 modes available
Verify package-level test organization with JUnit
Preconditions (2)
Step 1: Run all tests in the package 'com.example.utils'
Step 2: Verify that all test classes in 'com.example.utils' package are executed
Step 3: Run all tests in the package 'com.example.services'
Step 4: Verify that all test classes in 'com.example.services' package are executed
✅ Expected Result: All tests in the specified packages run successfully and report correct pass/fail status
Automation Requirements - JUnit 5
Assertions Needed:
Verify test execution count matches number of test classes in package
Verify all tests pass in the package
Best Practices:
Use JUnit Platform Launcher API to run tests by package
Use assertions to verify test results
Organize tests in packages matching source code structure
Automated Solution
JUnit
import org.junit.platform.engine.DiscoverySelector;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.LauncherFactory;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
import org.junit.platform.launcher.listeners.TestExecutionSummary;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class PackageLevelTestRunner {

    public static void main(String[] args) {
        runTestsInPackage("com.example.utils");
        runTestsInPackage("com.example.services");
    }

    private static void runTestsInPackage(String packageName) {
        LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
                .selectors(DiscoverySelectors.selectPackage(packageName))
                .build();

        Launcher launcher = LauncherFactory.create();
        SummaryGeneratingListener listener = new SummaryGeneratingListener();

        launcher.registerTestExecutionListeners(listener);
        launcher.execute(request);

        TestExecutionSummary summary = listener.getSummary();

        System.out.println("Package: " + packageName);
        System.out.println("Tests found: " + summary.getTestsFoundCount());
        System.out.println("Tests succeeded: " + summary.getTestsSucceededCount());
        System.out.println("Tests failed: " + summary.getTestsFailedCount());

        // Assertions to verify all tests passed
        assertTrue(summary.getTestsFoundCount() > 0, "No tests found in package " + packageName);
        assertEquals(0, summary.getTestsFailedCount(), "Some tests failed in package " + packageName);
    }
}

This code uses JUnit 5's Launcher API to run all tests in a given package.

The runTestsInPackage method builds a discovery request selecting the package, then executes it with a launcher.

A SummaryGeneratingListener collects test results.

After execution, it prints the number of tests found, succeeded, and failed.

Assertions check that at least one test was found and that no tests failed.

This approach ensures package-level test organization is verified by running all tests in the package and checking their results.

Common Mistakes - 3 Pitfalls
Using hardcoded test class selectors instead of package selectors
Not checking if any tests were found before asserting success
Ignoring test failures and not asserting on them
Bonus Challenge

Now add data-driven testing with 3 different package names to run tests dynamically

Show Hint