in junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassBasedTestDescriptor.java [294:336]
private Object invokeTestInstanceFactory(Optional<Object> outerInstance, ExtensionContext extensionContext) {
Object instance;
try {
instance = this.testInstanceFactory.createTestInstance(
new DefaultTestInstanceFactoryContext(this.testClass, outerInstance), extensionContext);
}
catch (Throwable throwable) {
UnrecoverableExceptions.rethrowIfUnrecoverable(throwable);
if (throwable instanceof TestInstantiationException) {
throw (TestInstantiationException) throwable;
}
String message = String.format("TestInstanceFactory [%s] failed to instantiate test class [%s]",
this.testInstanceFactory.getClass().getName(), this.testClass.getName());
if (StringUtils.isNotBlank(throwable.getMessage())) {
message += ": " + throwable.getMessage();
}
throw new TestInstantiationException(message, throwable);
}
if (!this.testClass.isInstance(instance)) {
String testClassName = this.testClass.getName();
Class<?> instanceClass = (instance == null ? null : instance.getClass());
String instanceClassName = (instanceClass == null ? "null" : instanceClass.getName());
// If the test instance was loaded via a different ClassLoader, append
// the identity hash codes to the type names to help users disambiguate
// between otherwise identical "fully qualified class names".
if (testClassName.equals(instanceClassName)) {
testClassName += "@" + Integer.toHexString(System.identityHashCode(this.testClass));
instanceClassName += "@" + Integer.toHexString(System.identityHashCode(instanceClass));
}
String message = String.format(
"TestInstanceFactory [%s] failed to return an instance of [%s] and instead returned an instance of [%s].",
this.testInstanceFactory.getClass().getName(), testClassName, instanceClassName);
throw new TestInstantiationException(message);
}
return instance;
}