Search in sources :

Example 1 with Inject

use of org.neo4j.test.extension.Inject in project neo4j by neo4j.

the class ActorsSupportExtension method postProcessTestInstance.

@Override
public void postProcessTestInstance(Object testInstance, ExtensionContext extensionContext) throws Exception {
    ActorsManager manager = (ActorsManager) getStore(extensionContext).getOrComputeIfAbsent(ACTOR_MANAGER, k -> new ActorsManager(extensionContext.getDisplayName()));
    Class<?> clazz = testInstance.getClass();
    List<Field> declaredFields = getAllFields(clazz);
    for (Field declaredField : declaredFields) {
        if (declaredField.getType() == Actor.class && declaredField.isAnnotationPresent(Inject.class)) {
            if (Modifier.isStatic(declaredField.getModifiers())) {
                throw new ExtensionConfigurationException(format("Actors cannot be inject into static field: %s.%s.", clazz.getName(), declaredField.getName()));
            }
            declaredField.setAccessible(true);
            if (declaredField.get(testInstance) != null) {
                throw new ExtensionConfigurationException(format("Field %s that is marked for injection in class %s is managed by extension container " + "and should not have any manually assigned value.", declaredField.getName(), clazz.getName()));
            }
            declaredField.set(testInstance, manager.createActor(declaredField.getName()));
        }
    }
}
Also used : PER_CLASS(org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Field(java.lang.reflect.Field) ExtensionConfigurationException(org.junit.jupiter.api.extension.ExtensionConfigurationException) String.format(java.lang.String.format) PER_METHOD(org.junit.jupiter.api.TestInstance.Lifecycle.PER_METHOD) AfterEachCallback(org.junit.jupiter.api.extension.AfterEachCallback) List(java.util.List) TestInstance(org.junit.jupiter.api.TestInstance) Inject(org.neo4j.test.extension.Inject) AfterAllCallback(org.junit.jupiter.api.extension.AfterAllCallback) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) ReflectionUtil.getAllFields(org.neo4j.test.ReflectionUtil.getAllFields) Namespace(org.junit.jupiter.api.extension.ExtensionContext.Namespace) TestInstancePostProcessor(org.junit.jupiter.api.extension.TestInstancePostProcessor) Inject(org.neo4j.test.extension.Inject) ExtensionConfigurationException(org.junit.jupiter.api.extension.ExtensionConfigurationException) Field(java.lang.reflect.Field)

Aggregations

String.format (java.lang.String.format)1 Field (java.lang.reflect.Field)1 Modifier (java.lang.reflect.Modifier)1 List (java.util.List)1 Optional (java.util.Optional)1 TestInstance (org.junit.jupiter.api.TestInstance)1 PER_CLASS (org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS)1 PER_METHOD (org.junit.jupiter.api.TestInstance.Lifecycle.PER_METHOD)1 AfterAllCallback (org.junit.jupiter.api.extension.AfterAllCallback)1 AfterEachCallback (org.junit.jupiter.api.extension.AfterEachCallback)1 ExtensionConfigurationException (org.junit.jupiter.api.extension.ExtensionConfigurationException)1 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)1 Namespace (org.junit.jupiter.api.extension.ExtensionContext.Namespace)1 TestInstancePostProcessor (org.junit.jupiter.api.extension.TestInstancePostProcessor)1 ReflectionUtil.getAllFields (org.neo4j.test.ReflectionUtil.getAllFields)1 Inject (org.neo4j.test.extension.Inject)1