下面列出了怎么用org.junit.jupiter.api.extension.TestInstances的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void beforeEach(ExtensionContext context) throws Exception {
IInjectorProvider injectorProvider = getOrCreateInjectorProvider(context);
if (injectorProvider instanceof IRegistryConfigurator) {
final IRegistryConfigurator registryConfigurator = (IRegistryConfigurator) injectorProvider;
registryConfigurator.setupRegistry();
}
if (injectorProvider != null) {
Injector injector = injectorProvider.getInjector();
if (injector != null) {
Object testInstance = context.getRequiredTestInstance();
injector.injectMembers(testInstance);
try {
TestInstances requiredTestInstances = context.getRequiredTestInstances();
for (Object o : requiredTestInstances.getEnclosingInstances()) {
injector.injectMembers(o);
}
} catch (NoSuchMethodError e) {
if (!Modifier.isStatic(testInstance.getClass().getModifiers())) {
if (testInstance.getClass().getDeclaringClass() != null) {
throw new ExtensionConfigurationException("Injection of nested classes needs Junit5 >= 5.4", e);
}
}
// OK, getRequiredTestInstances is not there in Junit5 < 5.4
}
}
}
}
@Override
public Optional<TestInstances> getTestInstances() {
throw new UnsupportedOperationException("not implemented");
}
@Override
public Optional<TestInstances> getTestInstances() {
return Optional.empty();
}
@Override
public Optional<TestInstances> getTestInstances() {
throw new UnsupportedOperationException();
}
@Override
public Optional<TestInstances> getTestInstances() {
throw new UnsupportedOperationException();
}
@Override
public Optional<TestInstances> getTestInstances() {
throw new UnsupportedOperationException();
}