Java Code Examples for org.junit.jupiter.api.extension.ExtensionContext#getTestInstance()

The following examples show how to use org.junit.jupiter.api.extension.ExtensionContext#getTestInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpringExtension.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public void beforeEach(ExtensionContext context) throws Exception {
    Object testInstance = context.getTestInstance();
    Method testMethod = context.getTestMethod()
        .get();
    getTestContextManager(context).beforeTestMethod(testInstance, testMethod);
}
 
Example 2
Source File: SpringExtension.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public void afterEach(ExtensionContext context) throws Exception {
    Object testInstance = context.getTestInstance();
    Method testMethod = context.getTestMethod()
        .get();
    Throwable testException = context.getExecutionException()
        .orElse(null);
    getTestContextManager(context).afterTestMethod(testInstance, testMethod, testException);
}
 
Example 3
Source File: JpaUnit.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
private TestInvocation createTestMethodInvocation(final ExtensionContext context, final boolean considerExceptions) {
    final JpaUnitContext ctx = JpaUnitContext.getInstance(context.getTestClass().get());

    return new TestInvocation() {

        @Override
        public Optional<Method> getTestMethod() {
            return context.getTestMethod();
        }

        @Override
        public ExecutionContext getContext() {
            return ctx;
        }

        @Override
        public Class<?> getTestClass() {
            return context.getTestClass().get();
        }

        @Override
        public Optional<Throwable> getException() {
            return considerExceptions ? context.getExecutionException() : Optional.empty();
        }

        @Override
        public FeatureResolver getFeatureResolver() {
            final FeatureResolver.Builder builder = FeatureResolver.newFeatureResolver(getTestClass());
            final Optional<Method> method = getTestMethod();
            if (method.isPresent()) {
                builder.withTestMethod(method.get());
            }
            return builder.build();
        }

        @Override
        public Optional<Object> getTestInstance() {
            return context.getTestInstance();
        }
    };
}