Challenge - 5 Problems
JUnit Suite Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of JUnit test suite execution
Given the following JUnit 5 test suite setup, what will be the output when running the suite?
JUnit
import org.junit.jupiter.api.Test; import org.junit.platform.suite.api.SelectClasses; import org.junit.platform.suite.api.Suite; @Suite @SelectClasses({TestA.class, TestB.class}) public class MyTestSuite {} class TestA { @Test void testA1() { System.out.println("TestA1 executed"); } } class TestB { @Test void testB1() { System.out.println("TestB1 executed"); } }
Attempts:
2 left
💡 Hint
Remember that @Suite runs all tests in the selected classes in the order they are declared.
✗ Incorrect
The @Suite annotation with @SelectClasses runs all tests in the specified classes. TestA's test runs first, printing 'TestA1 executed', then TestB's test runs, printing 'TestB1 executed'.
❓ assertion
intermediate2:00remaining
Correct assertion to verify test suite execution count
You have a JUnit 5 test suite with two test classes, each containing two tests. Which assertion correctly verifies that the suite ran exactly 4 tests?
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.platform.launcher.Launcher; import org.junit.platform.launcher.LauncherDiscoveryRequest; import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; import org.junit.platform.launcher.core.LauncherFactory; import org.junit.platform.engine.discovery.DiscoverySelectors; import org.junit.platform.launcher.TestExecutionSummary; import org.junit.platform.launcher.listeners.SummaryGeneratingListener; LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() .selectors(DiscoverySelectors.selectClass(MyTestSuite.class)) .build(); Launcher launcher = LauncherFactory.create(); SummaryGeneratingListener listener = new SummaryGeneratingListener(); launcher.execute(request, listener); TestExecutionSummary summary = listener.getSummary(); // Which assertion is correct here?
Attempts:
2 left
💡 Hint
Count all successful tests run by the suite.
✗ Incorrect
Since the suite contains 2 classes with 2 tests each, total tests run and succeeded should be 4. The assertion verifying testsSucceededCount equals 4 is correct.
🔧 Debug
advanced2:00remaining
Identify the cause of suite not running tests
Given this JUnit 5 test suite, why does running MyTestSuite not execute any tests?
JUnit
import org.junit.platform.suite.api.SelectClasses; import org.junit.platform.suite.api.Suite; import org.junit.jupiter.api.Test; @Suite @SelectClasses({TestX.class}) public class MyTestSuite {} class TestX { void testX1() { System.out.println("TestX1 executed"); } }
Attempts:
2 left
💡 Hint
JUnit 5 requires test methods to be annotated with @Test to be recognized.
✗ Incorrect
The test method testX1 lacks the @Test annotation, so JUnit does not recognize it as a test and does not run it.
❓ framework
advanced2:00remaining
Understanding @Suite and test discovery in JUnit 5
Which statement about JUnit 5's @Suite and test discovery is TRUE?
Attempts:
2 left
💡 Hint
Think about how JUnit 5 knows which tests to run in a suite.
✗ Incorrect
JUnit 5's @Suite requires explicit selection of test classes or packages using annotations like @SelectClasses or @SelectPackages. It does not auto-discover all tests.
🧠 Conceptual
expert3:00remaining
Best practice for organizing large test suites with @Suite
You have a large project with hundreds of test classes. What is the best practice to organize test suites using @Suite to keep tests maintainable and efficient?
Attempts:
2 left
💡 Hint
Think about scalability and ease of maintenance.
✗ Incorrect
Organizing tests into smaller suites by feature or module improves maintainability and allows selective test runs. A master suite can aggregate these smaller suites for full runs.