public final Builder addResult(TestIdentifier identifier, TestExecutionResult result) { DisplayName displayName = getDisplayName(identifier); if (identifier.isTest()) { testsBuilder().add(displayName); } switch (result.getStatus()) { case SUCCESSFUL: successfulBuilder().add(displayName); return this; case FAILED: failuresBuilder().put(displayName, result.getThrowable().orElse(null)); return this; default: throw new AssertionError("Unhandled case in enum: " + result.getStatus()); } }
@org.junit.jupiter.api.Test void testWithExpectedException_successfulTest_fails() { ExecutionEventRecorder eventRecorder = executeTests(TestTestCase.class, "testWithExpectedException_successfulTest"); assertThat(eventRecorder.getTestStartedCount()).isEqualTo(1); assertThat(eventRecorder.getTestFailedCount()).isEqualTo(1); //@formatter:off Optional<String> failedTestMessage = eventRecorder .getFailedTestFinishedEvents().get(0) .getPayload(TestExecutionResult.class) .flatMap(TestExecutionResult::getThrowable) .map(Throwable::getMessage); //@formatter:on String expectedMessage = format(EXPECTED_EXCEPTION_WAS_NOT_THROWN, IllegalArgumentException.class); assertThat(failedTestMessage).contains(expectedMessage); }
@org.junit.jupiter.api.Test void testWithExpectedException_exceptionThrownOfSupertype_fails() { ExecutionEventRecorder eventRecorder = executeTests(TestTestCase.class, "testWithExpectedException_exceptionThrownOfSupertype"); assertThat(eventRecorder.getTestStartedCount()).isEqualTo(1); assertThat(eventRecorder.getTestFailedCount()).isEqualTo(1); //@formatter:off Optional<Throwable> failedTestThrowable = eventRecorder .getFailedTestFinishedEvents().get(0) .getPayload(TestExecutionResult.class) .flatMap(TestExecutionResult::getThrowable); //@formatter:on assertThat(failedTestThrowable).containsInstanceOf(RuntimeException.class); }
@org.junit.jupiter.api.Test void testWithTimeout_exceedsTimeout_fails() throws Exception { ExecutionEventRecorder eventRecorder = executeTests(TestTestCase.class, "testWithTimeout_exceedsTimeout"); assertThat(eventRecorder.getTestStartedCount()).isEqualTo(1); assertThat(eventRecorder.getTestFailedCount()).isEqualTo(1); //@formatter:off Optional<String> failedTestMessage = eventRecorder .getFailedTestFinishedEvents().get(0) .getPayload(TestExecutionResult.class) .flatMap(TestExecutionResult::getThrowable) .map(Throwable::getMessage); String expectedMessage = String.format( TimeoutExtension.TEST_RAN_TOO_LONG, "testWithTimeout_exceedsTimeout()", 1, 10); //@formatter:on // the message contains the actual run time, which is unpredictable, so it has to be cut off for the assertion String expectedKnownPrefix = expectedMessage.substring(0, expectedMessage.length() - 6); assertThat(failedTestMessage).isNotEmpty(); assertThat(failedTestMessage.get()).startsWith(expectedKnownPrefix); }
@Override public void executionFinished(final TestIdentifier testIdentifier, final TestExecutionResult testExecutionResult) { if (testIdentifier.isTest()) { final String uuid = tests.get(); tests.remove(); getLifecycle().updateTestCase(uuid, result -> { result.setStage(Stage.FINISHED); switch (testExecutionResult.getStatus()) { case FAILED: testExecutionResult.getThrowable().ifPresent(throwable -> { result.setStatus(getStatus(throwable)); result.setStatusDetails(ResultsUtils.getStatusDetails(throwable).orElse(null)); }); break; case SUCCESSFUL: result.setStatus(PASSED); break; default: result.setStatus(SKIPPED); testExecutionResult.getThrowable().ifPresent(throwable -> result.setStatusDetails(ResultsUtils.getStatusDetails(throwable).orElse(null)) ); break; } }); getLifecycle().stopTestCase(uuid); getLifecycle().writeTestCase(uuid); } }
@Override public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) { super.executionFinished(testIdentifier, testExecutionResult); if (testIdentifier.isTest()) { Status status = testExecutionResult.getStatus(); Color color = Color.valueOf(status); printMessage(color, STATUS + status); printFailureMessage(testIdentifier, testExecutionResult, color); System.out.println(); } }
@Override public void executionFinished(TestDescriptor testDescriptor, TestExecutionResult testExecutionResult) { Optional<Throwable> throwable = testExecutionResult.getThrowable(); if (throwable.isPresent()) { throw new UndeclaredThrowableException(throwable.get()); } }
@Override public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) { Map<String, Object> data = new HashMap<>(); data.put("type", "executionFinished"); data.put("success", testExecutionResult.getStatus() == TestExecutionResult.Status.SUCCESSFUL); data.put("throwable", testExecutionResult.getThrowable().orElse(null)); writeData(testIdentifier, data); }
@Override public void execute(ExecutionRequest request) { TestDescriptor engine = request.getRootTestDescriptor(); EngineExecutionListener listener = request.getEngineExecutionListener(); listener.executionStarted(engine); for (TestDescriptor child : engine.getChildren()) { listener.executionStarted(child); listener.executionFinished(child, TestExecutionResult.successful()); } listener.executionFinished(engine, TestExecutionResult.successful()); }
@Override public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) { executionReportBuilder.addResult(testIdentifier, testExecutionResult); }
@Override public void executionFinished(TestDescriptor testDescriptor, TestExecutionResult result) { addEvent(ExecutionEvent.executionFinished(testDescriptor, result)); }
public Stream<ExecutionEvent> getFinishedEventsByStatus(Status status) { return getEventsByType(FINISHED) .filter( byPayload( TestExecutionResult.class, where(TestExecutionResult::getStatus, isEqual(status)))); }
public static ExecutionEvent executionFinished( TestDescriptor testDescriptor, TestExecutionResult result) { return new ExecutionEvent(FINISHED, testDescriptor, result); }
private void printFailureMessage(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult, Color color) { if (testExecutionResult.getStatus().equals(Status.FAILED)) { testExecutionResult.getThrowable() .ifPresent(throwable -> printMessage(color, throwable.toString())); } }
@Override public void executionFinished(TestDescriptor testDescriptor, TestExecutionResult testExecutionResult) { allTestsPassed = allTestsPassed && testExecutionResult.getStatus() == SUCCESSFUL; }
public static ExecutionEvent executionFinished(TestDescriptor testDescriptor, TestExecutionResult result) { return new ExecutionEvent(FINISHED, testDescriptor, result); }
public static Condition<TestExecutionResult> status(Status expectedStatus) { return new Condition<>(where(TestExecutionResult::getStatus, isEqual(expectedStatus)), "status is %s", expectedStatus); }
public static Condition<TestExecutionResult> cause(Condition<? super Throwable> condition) { return new Condition<TestExecutionResult>(where(TestExecutionResult::getThrowable, throwable -> { return throwable.isPresent() && condition.matches(throwable.get()); }), "cause where %s", condition); }
public static Condition<ExecutionEvent> finished(Condition<TestExecutionResult> resultCondition) { return allOf(type(FINISHED), result(resultCondition)); }
public static Condition<ExecutionEvent> result(Condition<TestExecutionResult> condition) { return new Condition<>(byPayload(TestExecutionResult.class, condition::matches), "event with result where %s", condition); }
private Stream<ExecutionEvent> testFinishedEvents(Status status) { return testEventsByType(FINISHED).filter( byPayload(TestExecutionResult.class, where(TestExecutionResult::getStatus, isEqual(status)))); }