Search in sources :

Example 1 with TestInstantiationException

use of org.junit.jupiter.api.extension.TestInstantiationException in project junit5 by junit-team.

the class ClassBasedTestDescriptor method invokeTestInstanceFactory.

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());
        // 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;
}
Also used : TestInstantiationException(org.junit.jupiter.api.extension.TestInstantiationException)

Aggregations

TestInstantiationException (org.junit.jupiter.api.extension.TestInstantiationException)1