Skip to main content

On This Page

A Guide to Engine Test Kit in Junit 5

2 min read
Share

These articles are AI-generated summaries. Please check the original sources for full details.

A Guide to Engine Test Kit in Junit 5

JUnit 5’s Engine Test Kit allows developers to execute test plans and gather detailed statistics. The example test suite includes four tests with varying outcomes: one pass, one fail, one skip, and one abort.

Why This Matters

While ideal models assume tests run perfectly, real-world scenarios involve failures, skips, and aborts. Undetected issues in test execution can mask bugs, leading to unreliable CI/CD pipelines. The Engine Test Kit provides visibility into these edge cases, reducing the risk of undetected failures that might otherwise cost hours in debugging.

Key Insights

Working Example

public class Display {
    private final Platform platform;
    private final int height;
    // standard constructor, getters and setters
}
public enum Platform {
    DESKTOP,
    MOBILE
}
public class DisplayTest {
    private final Display display = new Display(Platform.DESKTOP, 1000);
    @Test
    void whenCorrect_thenSucceeds() {
        assertEquals(1000, display.getHeight());
    }
    @Test
    void whenIncorrect_thenFails() {
        assertEquals(500, display.getHeight());
    }
    @Test
    @Disabled("Flakey test needs investigating")
    void whenDisabled_thenSkips() {
        assertEquals(999, display.getHeight());
    }
    @Test
    void whenAssumptionsFail_thenAborts() {
        assumeTrue(display.getPlatform() == Platform.MOBILE, "test only runs for mobile");
    }
}
@Test
void givenTestSuite_whenRunningAllTests_thenCollectHighLevelStats() {
    EngineTestKit
        .engine("junit-jupiter")
        .selectors(selectClass(DisplayTest.class))
        .execute()
        .testEvents()
        .assertStatistics(stats ->
            stats.started(3).finished(3).succeeded(1).failed(1).skipped(1).aborted(1));
}

Practical Applications

  • Use Case: Test suite validation in CI/CD pipelines
  • Pitfall: Overlooking dynamic test registration events leading to incomplete stats

References:


Continue reading

Next article

Amazon EKS Adds Native Support for AWS Secrets Store CSI Driver Provider

Related Content