Exemple #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);
    }
  }
Exemple #2
0
  @Nullable
  private Object getTestedObjectFromFieldInTestClassIfApplicable(
      @Nonnull Object testClassInstance) {
    Object testedObject = null;

    if (!createAutomatically) {
      testedObject = getFieldValue(testedField, testClassInstance);
      createAutomatically = testedObject == null && !isFinal(testedField.getModifiers());
    }

    return testedObject;
  }
Exemple #3
0
  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);
    }
  }
Exemple #4
0
 @Nonnull
 Class<?> getDeclaringTestClass() {
   return testedField.getDeclaringClass();
 }