下面列出了org.junit.jupiter.api.extension.ExtensionContext#getTestInstance() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void beforeEach(ExtensionContext context) throws Exception {
Object testInstance = context.getTestInstance();
Method testMethod = context.getTestMethod()
.get();
getTestContextManager(context).beforeTestMethod(testInstance, testMethod);
}
@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);
}
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();
}
};
}