Search in sources :

Example 6 with FalsePositiveException

use of org.evosuite.runtime.FalsePositiveException in project evosuite by EvoSuite.

the class Injector method validateBean.

/**
 * Check if all beans that need injection have been properly injected
 *
 * @param instance
 * @param clazz
 * @param <T>
 * @throws FalsePositiveException
 * @throws IllegalArgumentException
 */
@Constraints(noNullInputs = true, notMutable = true, noDirectInsertion = true)
public static <T> void validateBean(@BoundInputVariable(initializer = true, atMostOnceWithSameParameters = true) T instance, Class<?> clazz) throws FalsePositiveException, IllegalArgumentException {
    Inputs.checkNull(instance, clazz);
    for (Field f : getAllFieldsToInject(clazz)) {
        f.setAccessible(true);
        try {
            Object obj = f.get(instance);
            if (obj == null) {
                throw new FalsePositiveException("Missing dependency injection for field " + f.getName() + " in class " + clazz.getName());
            }
        // it might be a bean with its own dependency injections.
        // but those should be handled in its instantiation
        } catch (IllegalAccessException e) {
            // shouldn't really happen
            logger.warn(e.toString());
        }
    }
    Class<?> parent = clazz.getSuperclass();
    if (parent != null && !parent.equals(Object.class)) {
        validateBean(instance, parent);
    }
}
Also used : FalsePositiveException(org.evosuite.runtime.FalsePositiveException) Field(java.lang.reflect.Field) Constraints(org.evosuite.runtime.annotation.Constraints)

Aggregations

FalsePositiveException (org.evosuite.runtime.FalsePositiveException)6 EJB (javax.ejb.EJB)4 Test (org.junit.Test)4 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)1 Constraints (org.evosuite.runtime.annotation.Constraints)1 InstrumentedClass (org.evosuite.runtime.instrumentation.InstrumentedClass)1 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)1 EvosuiteError (org.evosuite.testcase.execution.EvosuiteError)1 UncompilableCodeException (org.evosuite.testcase.execution.UncompilableCodeException)1 MethodDescriptor (org.evosuite.testcase.fm.MethodDescriptor)1 VariableReference (org.evosuite.testcase.variable.VariableReference)1 GenericAccessibleObject (org.evosuite.utils.generic.GenericAccessibleObject)1 GenericClass (org.evosuite.utils.generic.GenericClass)1 MockitoException (org.mockito.exceptions.base.MockitoException)1 InvalidUseOfMatchersException (org.mockito.exceptions.misusing.InvalidUseOfMatchersException)1 OngoingStubbing (org.mockito.stubbing.OngoingStubbing)1