Verify package-level test organization with JUnit
Preconditions (2)
✅ Expected Result: All tests in the specified packages run successfully and report correct pass/fail status
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.
Now add data-driven testing with 3 different package names to run tests dynamically
com.example.service.UserService?com.example.service. Tests should be in the same package but under the test source folder.com.example.service under the test source folder places the test class in com.example.service under the test source folder, which is correct.com.example.service under the test source folder -> Option Dsrc/main/java/com/app/utils/Calculator.java src/test/java/com/app/utils/CalculatorTest.java
com.app.utils?com.app.utils runs only tests in that package.CalculatorTest is in com.app.utils, so it will run; tests in other packages won't.CalculatorTest will run. -> Option Ccom.example.service.OrderServiceTest placed under src/main/java/com/example/service. What is the problem with this setup?Pass the test and this pattern moves unknown