void instantiateWithInjectableValues(@Nonnull Object testClassInstance) { if (isAvailableDuringSetup() && getFieldValue(testedField, testClassInstance) != null) { return; } injectionState.setTestedField(testedField); Object testedObject = getTestedObjectFromFieldInTestClassIfApplicable(testClassInstance); Class<?> testedClass = testedField.getType(); if (testedObject == null && createAutomatically) { if (reusePreviouslyCreatedInstance(testClassInstance, testedClass)) { return; } if (testedObjectCreation != null) { testedObject = testedObjectCreation.create(); setFieldValue(testedField, testClassInstance, testedObject); injectionState.saveTestedObject(testedClass, testedObject); } } else if (testedObject != null) { String dependencyKey = InjectionPoint.dependencyKey(testedClass, testedField.getName()); injectionState.saveTestedObject(dependencyKey, testedObject); testedClass = testedObject.getClass(); } if (testedObject != null) { performFieldInjection(testedClass, testedObject); executeInitializationMethodsIfAny(testedClass, testedObject); } }
@Nullable private Object getTestedObjectFromFieldInTestClassIfApplicable( @Nonnull Object testClassInstance) { Object testedObject = null; if (!createAutomatically) { testedObject = getFieldValue(testedField, testClassInstance); createAutomatically = testedObject == null && !isFinal(testedField.getModifiers()); } return testedObject; }
TestedField( @Nonnull InjectionState injectionState, @Nonnull Field field, @Nonnull Tested metadata) { this.injectionState = injectionState; testedField = field; this.metadata = metadata; fullInjection = metadata.fullyInitialized() ? new FullInjection(injectionState) : null; Class<?> fieldType = field.getType(); if (fieldType.isInterface()) { testedObjectCreation = null; } else { testedObjectCreation = new TestedObjectCreation(injectionState, fullInjection, field); injectionState.lifecycleMethods.findLifecycleMethods(fieldType); } }
@Nonnull Class<?> getDeclaringTestClass() { return testedField.getDeclaringClass(); }