Testing / JUnit6 Interview Questions
What is the TestExecutionListener SPI in JUnit 6?
The TestExecutionListener is a Service Provider Interface (SPI) that allows external tools (IDEs, build plugins, reporting frameworks) to observe test execution events without modifying test code. Unlike extensions (which are registered in test code), listeners are registered via ServiceLoader or programmatically through the launcher.
// Implement a custom TestExecutionListener: public class JsonReportListener implements TestExecutionListener { private final List<TestResult> results = new ArrayList<>(); @Override public void testStarted(TestIdentifier id) { System.out.println("Starting: " + id.getDisplayName()); } @Override public void executionFinished( TestIdentifier id, TestExecutionResult result) { results.add(new TestResult( id.getDisplayName(), result.getStatus() )); } @Override public void testPlanExecutionFinished(TestPlan plan) { // Write JSON report after all tests complete writeJsonReport(results); } } // Register via ServiceLoader (META-INF/services): // File: src/test/resources/META-INF/services/ // org.junit.platform.launcher.TestExecutionListener // Content: // com.example.JsonReportListener // Or register programmatically via LauncherDiscoveryRequest: LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder .request() .selectors(selectPackage("com.example")) .build(); Launcher launcher = LauncherFactory.create(); launcher.discover(request); launcher.execute(request, new JsonReportListener());
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
