Java 类org.junit.jupiter.api.extension.TestExtensionContext 实例源码

项目:demo-junit-5    文件:ExpectedExceptionExtension.java   
@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;
    }
}
项目:vaadin-016-helloworld-14    文件:TestcontainersExtension.java   
@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);
  }
项目:vaadin-016-helloworld-14    文件:TestcontainersExtension.java   
@Override
  public void afterEach(TestExtensionContext context) throws Exception {
//  public void afterEach(ExtensionContext context) throws Exception {
    testcontainer().apply(context).stop();
    removeTestcontainer().accept(context);
    removeWebDriver();
  }
项目:demo-junit-5    文件:ExpectedExceptionExtension.java   
@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
    }
}
项目:demo-junit-5    文件:TimeoutExtension.java   
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);
    }
}
项目:demo-junit-5    文件:RandomProvider.java   
@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);
}
项目:demo-junit-5    文件:DisabledIfTestFailedCondition.java   
@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);
}
项目:demo-junit-5    文件:BenchmarkExtension.java   
@Override
public void beforeTestExecution(TestExtensionContext context) {
    if (!shouldBeBenchmarked(context))
        return;

    storeNowAsLaunchTime(context, LaunchTimeKey.TEST);
}
项目:demo-junit-5    文件:BenchmarkExtension.java   
@Override
public void afterTestExecution(TestExtensionContext context) {
    if (!shouldBeBenchmarked(context))
        return;

    long launchTime = loadLaunchTime(context, LaunchTimeKey.TEST);
    long runtime = currentTimeMillis() - launchTime;
    report("Test", context, runtime);
}
项目:demo-junit-5    文件:CollectExceptionExtension.java   
@Override
public void handleTestExecutionException(TestExtensionContext context, Throwable throwable)
        throws Throwable {
    if (throwable instanceof Exception) {
        Exception exception = (Exception) throwable;
        getThrown(context).add(exception);
    }

    throw throwable;
}
项目:vaadin-016-helloworld-14    文件:ServletContainerExtension.java   
@Override
//  public void beforeEach(ExtensionContext context) throws Exception {
  public void beforeEach(TestExtensionContext context) throws Exception {
    Main.start();
  }
项目:vaadin-016-helloworld-14    文件:ServletContainerExtension.java   
@Override
//  public void afterEach(ExtensionContext context) throws Exception {
  public void afterEach(TestExtensionContext context) throws Exception {
   Main.shutdown();
  }
项目:demo-junit-5    文件:DisabledCondition.java   
@Override
public ConditionEvaluationResult evaluate(TestExtensionContext context) {
    return evaluateIfAnnotated(context.getElement());
}
项目:demo-junit-5    文件:TimeoutExtension.java   
@Override
public void beforeTestExecution(TestExtensionContext context) {
    storeNowAsLaunchTime(context);
}
项目:demo-junit-5    文件:TimeoutExtension.java   
@Override
public void afterTestExecution(TestExtensionContext context) {
    annotatedTimeout(context).ifPresent(timeout -> failTestIfRanTooLong(context, timeout));
}
项目:demo-junit-5    文件:TimeoutExtension.java   
private Optional<Long> annotatedTimeout(TestExtensionContext context) {
    return context.getElement()
            .flatMap(el -> findAnnotation(el, Test.class))
            .map(Test::timeout)
            .filter(timeout -> timeout != 0L);
}
项目:demo-junit-5    文件:OsCondition.java   
@Override
public ConditionEvaluationResult evaluate(TestExtensionContext context) {
    return evaluateIfAnnotated(context.getElement());
}
项目:demo-junit-5    文件:SimpleBenchmarkExtension.java   
@Override
public void beforeTestExecution(TestExtensionContext context) {
    storeNowAsLaunchTime(context);
}
项目:demo-junit-5    文件:SimpleBenchmarkExtension.java   
@Override
public void afterTestExecution(TestExtensionContext context) {
    long launchTime = loadLaunchTime(context);
    long elapsedTime = currentTimeMillis() - launchTime;
    report(context, elapsedTime);
}