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); } }
void clearIfAutomaticCreation() { if (createAutomatically) { injectionState.clearTestedObjectsAndInstantiatedDependencies(); if (!isAvailableDuringSetup()) { Object testClassInstance = injectionState.getCurrentTestClassInstance(); setFieldValue(testedField, testClassInstance, null); } } }
public void assignNewInstancesToTestedFields(@NotNull Object testClassInstance) { injectionState.buildListsOfInjectables(testClassInstance, injectableFields); TestedField previousField = null; for (TestedField testedField : testedFields) { if (previousField != null && !testedField.isAtSameLevelInTestClassHierarchy(previousField)) { injectionState.discardInjectablesFromLowerTestClassHierarchyLevels( testedField.getDeclaringTestClass()); } testedField.instantiateWithInjectableValues(testClassInstance); injectionState.resetConsumedInjectables(); previousField = testedField; } }
private boolean reusePreviouslyCreatedInstance( @Nonnull Object testClassInstance, @Nonnull Class<?> testedClass) { Object previousInstance = injectionState.getTestedInstance(testedClass); if (previousInstance != null) { setFieldValue(testedField, testClassInstance, previousInstance); return true; } return false; }