|
| 1 | +package com.baeldung.enginetestkit; |
| 2 | + |
| 3 | +import static java.util.Collections.emptyList; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; |
| 6 | +import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod; |
| 7 | +import static org.junit.platform.testkit.engine.EventConditions.abortedWithReason; |
| 8 | +import static org.junit.platform.testkit.engine.EventConditions.event; |
| 9 | +import static org.junit.platform.testkit.engine.EventConditions.finishedWithFailure; |
| 10 | +import static org.junit.platform.testkit.engine.EventConditions.skippedWithReason; |
| 11 | +import static org.junit.platform.testkit.engine.EventConditions.test; |
| 12 | +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf; |
| 13 | +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message; |
| 14 | + |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.junit.platform.testkit.engine.EngineDiscoveryResults; |
| 17 | +import org.junit.platform.testkit.engine.EngineTestKit; |
| 18 | +import org.junit.platform.testkit.engine.Events; |
| 19 | +import org.opentest4j.AssertionFailedError; |
| 20 | +import org.opentest4j.TestAbortedException; |
| 21 | + |
| 22 | +public class EngineTestKitDiscoveryUnitTest { |
| 23 | + |
| 24 | + @Test |
| 25 | + void givenJunitJupiterEngine_whenRunningTestSuite_thenTestsAreDiscovered() { |
| 26 | + EngineDiscoveryResults results = EngineTestKit.engine("junit-jupiter") |
| 27 | + .selectors(selectClass(DisplayTest.class)) |
| 28 | + .discover(); |
| 29 | + |
| 30 | + assertEquals("JUnit Jupiter", results.getEngineDescriptor().getDisplayName()); |
| 31 | + assertEquals(emptyList(), results.getDiscoveryIssues()); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + void givenJunitVintageEngine_whenRunningTestSuite_thenTestsAreDiscovered() { |
| 36 | + EngineDiscoveryResults results = EngineTestKit.engine("junit-vintage") |
| 37 | + .selectors(selectClass(DisplayTest.class)) |
| 38 | + .discover(); |
| 39 | + |
| 40 | + assertEquals("JUnit Vintage", results.getEngineDescriptor().getDisplayName()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void givenTestSuite_whenRunningAllTests_thenCollectHighLevelStats() { |
| 45 | + EngineTestKit |
| 46 | + .engine("junit-jupiter") |
| 47 | + .selectors(selectClass(DisplayTest.class)) |
| 48 | + .execute() |
| 49 | + .testEvents() |
| 50 | + .assertStatistics(stats -> |
| 51 | + stats.started(3).finished(3).succeeded(1).failed(1).skipped(1).aborted(1)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void givenTestSuite_whenRunningTestThatAborts_thenCollectDetailedStats() { |
| 56 | + Events testEvents = EngineTestKit |
| 57 | + .engine("junit-jupiter") |
| 58 | + .selectors(selectMethod(DisplayTest.class, "whenAssumptionsFail_thenAborts")) |
| 59 | + .execute() |
| 60 | + .testEvents(); |
| 61 | + |
| 62 | + testEvents.assertThatEvents() |
| 63 | + .haveExactly(1, event(test("whenAssumptionsFail_thenAborts"), |
| 64 | + abortedWithReason(instanceOf(TestAbortedException.class), |
| 65 | + message(message -> message.contains("test only runs for mobile"))))); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void givenTestSuite_whenRunningTestThatFails_thenCollectDetailedStats() { |
| 70 | + Events testEvents = EngineTestKit |
| 71 | + .engine("junit-jupiter") |
| 72 | + .selectors(selectMethod(DisplayTest.class, "whenIncorrect_thenFails")) |
| 73 | + .execute() |
| 74 | + .testEvents(); |
| 75 | + |
| 76 | + testEvents.assertThatEvents() |
| 77 | + .haveExactly(1, event(test("whenIncorrect_thenFails"), |
| 78 | + finishedWithFailure(instanceOf(AssertionFailedError.class)))); |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments