@Override public void handleTestExecutionException(TestExtensionContext context, Throwable throwable) throws Throwable { boolean throwableMatchesExpectedException = expectedException(context) .filter(expected -> expected.isInstance(throwable)) .isPresent(); // in the `afterTestExecution` callback we have to pass or fail the test // depending on whether the exception was thrown or not; // to do that we need to register whether the exception was thrown; // (NOTE that if no exception was thrown, NOTHING is registered) if (throwableMatchesExpectedException) storeExceptionStatus(context, EXCEPTION.WAS_THROWN_AS_EXPECTED); else { // this extension is not in charge of the throwable, so we need to rethrow; storeExceptionStatus(context, EXCEPTION.WAS_THROWN_NOT_AS_EXPECTED); throw throwable; } }
@Override // public void beforeEach(ExtensionContext context) throws Exception { public void beforeEach(TestExtensionContext context) throws Exception { BrowserWebDriverContainer webDriverContainer = new BrowserWebDriverContainer() .withDesiredCapabilities(DesiredCapabilities.chrome()); // only one per container webDriverContainer.start(); Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(LoggerFactory.getLogger(this.getClass())); webDriverContainer.followOutput(logConsumer); storeTestcontainer().accept(context, webDriverContainer); storeWebDriver().accept(context, webDriverContainer::getWebDriver); }
@Override public void afterEach(TestExtensionContext context) throws Exception { // public void afterEach(ExtensionContext context) throws Exception { testcontainer().apply(context).stop(); removeTestcontainer().accept(context); removeWebDriver(); }
@Override public void afterTestExecution(TestExtensionContext context) throws Exception { switch(loadExceptionStatus(context)) { case WAS_NOT_THROWN: expectedException(context) .map(expected -> new IllegalStateException("Expected exception " + expected + " was not thrown.")) .ifPresent(ex -> { throw ex; }); case WAS_THROWN_AS_EXPECTED: // the exception was thrown as expected so there is nothing to do case WAS_THROWN_NOT_AS_EXPECTED: // an exception was thrown but of the wrong type; // it was rethrown in `handleTestExecutionException` // so there is nothing to do here } }
private void failTestIfRanTooLong(TestExtensionContext context, Long timeout) { long launchTime = loadLaunchTime(context); long elapsedTime = currentTimeMillis() - launchTime; if (elapsedTime > timeout) { String message = format( "Test '%s' was supposed to run no longer than %d ms but ran %d ms.", context.getDisplayName(), timeout, elapsedTime); throw new IllegalStateException(message); } }
@Override public void handleTestExecutionException( TestExtensionContext context, Throwable throwable) throws Throwable { String seed = Optional.ofNullable(randomByUniqueId(context).get(context.getUniqueId())) .map(SeededRandom::seed) .map(s -> "seed " + s) .orElse("unknown seed"); System.out.println("Exception occurred in test based on " + seed); }
@Override public ConditionEvaluationResult evaluate(TestExtensionContext context) { Class<? extends Exception>[] exceptionTypes = context.getTestClass() .flatMap(testClass -> findAnnotation(testClass, DisabledIfTestFailedWith.class)) .orElseThrow(() -> new IllegalStateException("The extension should not be executed " + "unless the test class is annotated with @DisabledIfTestFailedWith.")) .value(); return disableIfExceptionWasThrown(context, exceptionTypes); }
@Override public void beforeTestExecution(TestExtensionContext context) { if (!shouldBeBenchmarked(context)) return; storeNowAsLaunchTime(context, LaunchTimeKey.TEST); }
@Override public void afterTestExecution(TestExtensionContext context) { if (!shouldBeBenchmarked(context)) return; long launchTime = loadLaunchTime(context, LaunchTimeKey.TEST); long runtime = currentTimeMillis() - launchTime; report("Test", context, runtime); }
@Override public void handleTestExecutionException(TestExtensionContext context, Throwable throwable) throws Throwable { if (throwable instanceof Exception) { Exception exception = (Exception) throwable; getThrown(context).add(exception); } throw throwable; }
@Override // public void beforeEach(ExtensionContext context) throws Exception { public void beforeEach(TestExtensionContext context) throws Exception { Main.start(); }
@Override // public void afterEach(ExtensionContext context) throws Exception { public void afterEach(TestExtensionContext context) throws Exception { Main.shutdown(); }
@Override public ConditionEvaluationResult evaluate(TestExtensionContext context) { return evaluateIfAnnotated(context.getElement()); }
@Override public void beforeTestExecution(TestExtensionContext context) { storeNowAsLaunchTime(context); }
@Override public void afterTestExecution(TestExtensionContext context) { annotatedTimeout(context).ifPresent(timeout -> failTestIfRanTooLong(context, timeout)); }
private Optional<Long> annotatedTimeout(TestExtensionContext context) { return context.getElement() .flatMap(el -> findAnnotation(el, Test.class)) .map(Test::timeout) .filter(timeout -> timeout != 0L); }
@Override public void afterTestExecution(TestExtensionContext context) { long launchTime = loadLaunchTime(context); long elapsedTime = currentTimeMillis() - launchTime; report(context, elapsedTime); }