예제 #1
0
  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);
    }
  }
예제 #2
0
  void clearIfAutomaticCreation() {
    if (createAutomatically) {
      injectionState.clearTestedObjectsAndInstantiatedDependencies();

      if (!isAvailableDuringSetup()) {
        Object testClassInstance = injectionState.getCurrentTestClassInstance();
        setFieldValue(testedField, testClassInstance, null);
      }
    }
  }
예제 #3
0
  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;
  }