private FieldInitializationReport acquireFieldInstance() throws IllegalAccessException { Object fieldInstance = field.get(fieldOwner); if(fieldInstance != null) { return new FieldInitializationReport(fieldInstance, false, false); } return instantiator.instantiate(); }
public boolean processInjection(Field injectMocksField, Object injectMocksFieldOwner, Set<Object> mockCandidates) { FieldInitializationReport report = initializeInjectMocksField(injectMocksField, injectMocksFieldOwner); // for each field in the class hierarchy boolean injectionOccurred = false; Class<?> fieldClass = report.fieldClass(); Object fieldInstanceNeedingInjection = report.fieldInstance(); while (fieldClass != Object.class) { injectionOccurred |= injectMockCandidates(fieldClass, fieldInstanceNeedingInjection, newMockSafeHashSet(mockCandidates)); fieldClass = fieldClass.getSuperclass(); } return injectionOccurred; }
try { FieldInitializationReport report = new FieldInitializer(testInstance, field).initialize(); instance = report.fieldInstance(); } catch (MockitoException e) { new Reporter().cannotInitializeForSpyAnnotation(field.getName(), e);
public boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) { try { SimpleArgumentResolver simpleArgumentResolver = new SimpleArgumentResolver(mockCandidates); FieldInitializationReport report = new FieldInitializer(fieldOwner, field, simpleArgumentResolver).initialize(); return report.fieldWasInitializedUsingContructorArgs(); } catch (MockitoException e) { if(e.getCause() instanceof InvocationTargetException) { Throwable realCause = e.getCause().getCause(); throw fieldInitialisationThrewException(field, realCause); } // other causes should be fine return false; } }
public boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) { try { SimpleArgumentResolver simpleArgumentResolver = new SimpleArgumentResolver(mockCandidates); FieldInitializationReport report = new FieldInitializer(fieldOwner, field, simpleArgumentResolver).initialize(); return report.fieldWasInitialized(); } catch (MockitoException e) { if(e.getCause() instanceof InvocationTargetException) { Throwable realCause = e.getCause().getCause(); new Reporter().fieldInitialisationThrewException(field, realCause); } return false; } }
try { FieldInitializationReport report = new FieldInitializer(testInstance, field).initialize(); instance = report.fieldInstance(); } catch (MockitoException e) { new Reporter().cannotInitializeForSpyAnnotation(field.getName(), e);
public boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) { try { SimpleArgumentResolver simpleArgumentResolver = new SimpleArgumentResolver(mockCandidates); FieldInitializationReport report = new FieldInitializer(fieldOwner, field, simpleArgumentResolver).initialize(); return report.fieldWasInitializedUsingContructorArgs(); } catch (MockitoException e) { if(e.getCause() instanceof InvocationTargetException) { Throwable realCause = e.getCause().getCause(); new Reporter().fieldInitialisationThrewException(field, realCause); } // other causes should be fine return false; } }
public boolean processInjection(Field injectMocksField, Object injectMocksFieldOwner, Set<Object> mockCandidates) { // Set<Object> mocksToBeInjected = new HashSet<Object>(mockCandidates); FieldInitializationReport report = initializeInjectMocksField(injectMocksField, injectMocksFieldOwner); // for each field in the class hierarchy boolean injectionOccurred = false; Class<?> fieldClass = report.fieldClass(); Object fieldInstanceNeedingInjection = report.fieldInstance(); while (fieldClass != Object.class) { injectionOccurred |= injectMockCandidates(fieldClass, newMockSafeHashSet(mockCandidates), fieldInstanceNeedingInjection); fieldClass = fieldClass.getSuperclass(); } return injectionOccurred; }
private FieldInitializationReport acquireFieldInstance() throws IllegalAccessException { Object fieldInstance = field.get(fieldOwner); if(fieldInstance != null) { return new FieldInitializationReport(fieldInstance, false, false); } return instantiator.instantiate(); }
public boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) { Set<Object> mocksToBeInjected = new HashSet<Object>(mockCandidates); FieldInitializationReport report = null; try { report = new FieldInitializer(fieldOwner, field).initialize(); } catch (MockitoException e) { if(e.getCause() instanceof InvocationTargetException) { Throwable realCause = e.getCause().getCause(); new Reporter().fieldInitialisationThrewException(field, realCause); } new Reporter().cannotInitializeForInjectMocksAnnotation(field.getName(), e); } // for each field in the class hierarchy boolean injectionOccurred = false; Class<?> fieldClass = report.fieldClass(); Object fieldInstanceNeedingInjection = report.fieldInstance(); while (fieldClass != Object.class) { injectionOccurred |= injectMockCandidate(fieldClass, mocksToBeInjected, fieldInstanceNeedingInjection); fieldClass = fieldClass.getSuperclass(); } return injectionOccurred; }
public FieldInitializationReport instantiate() { final AccessibilityChanger changer = new AccessibilityChanger(); Constructor<?> constructor = null; try { constructor = field.getType().getDeclaredConstructor(); changer.enableAccess(constructor); final Object[] noArg = new Object[0]; Object newFieldInstance = constructor.newInstance(noArg); setField(testClass, field,newFieldInstance); return new FieldInitializationReport(field.get(testClass), true, false); } catch (NoSuchMethodException e) { throw new MockitoException("the type '" + field.getType().getSimpleName() + "' has no default constructor", e); } catch (InvocationTargetException e) { throw new MockitoException("the default constructor of type '" + field.getType().getSimpleName() + "' has raised an exception (see the stack trace for cause): " + e.getTargetException().toString(), e); } catch (InstantiationException e) { throw new MockitoException("InstantiationException (see the stack trace for cause): " + e.toString(), e); } catch (IllegalAccessException e) { throw new MockitoException("IllegalAccessException (see the stack trace for cause): " + e.toString(), e); } finally { if(constructor != null) { changer.safelyDisableAccess(constructor); } } } }
public FieldInitializationReport instantiate() { final AccessibilityChanger changer = new AccessibilityChanger(); Constructor<?> constructor = null; try { constructor = biggestConstructor(field.getType()); changer.enableAccess(constructor); final Object[] args = argResolver.resolveTypeInstances(constructor.getParameterTypes()); Object newFieldInstance = constructor.newInstance(args); setField(testClass, field,newFieldInstance); return new FieldInitializationReport(field.get(testClass), false, true); } catch (IllegalArgumentException e) { throw new MockitoException("internal error : argResolver provided incorrect types for constructor " + constructor + " of type " + field.getType().getSimpleName(), e); } catch (InvocationTargetException e) { throw new MockitoException("the constructor of type '" + field.getType().getSimpleName() + "' has raised an exception (see the stack trace for cause): " + e.getTargetException().toString(), e); } catch (InstantiationException e) { throw new MockitoException("InstantiationException (see the stack trace for cause): " + e.toString(), e); } catch (IllegalAccessException e) { throw new MockitoException("IllegalAccessException (see the stack trace for cause): " + e.toString(), e); } finally { if(constructor != null) { changer.safelyDisableAccess(constructor); } } }
public FieldInitializationReport instantiate() { final AccessibilityChanger changer = new AccessibilityChanger(); Constructor<?> constructor = null; try { constructor = field.getType().getDeclaredConstructor(); changer.enableAccess(constructor); final Object[] noArg = new Object[0]; Object newFieldInstance = constructor.newInstance(noArg); new FieldSetter(testClass, field).set(newFieldInstance); return new FieldInitializationReport(field.get(testClass), true, false); } catch (NoSuchMethodException e) { throw new MockitoException("the type '" + field.getType().getSimpleName() + "' has no default constructor", e); } catch (InvocationTargetException e) { throw new MockitoException("the default constructor of type '" + field.getType().getSimpleName() + "' has raised an exception (see the stack trace for cause): " + e.getTargetException().toString(), e); } catch (InstantiationException e) { throw new MockitoException("InstantiationException (see the stack trace for cause): " + e.toString(), e); } catch (IllegalAccessException e) { throw new MockitoException("IllegalAccessException (see the stack trace for cause): " + e.toString(), e); } finally { if(constructor != null) { changer.safelyDisableAccess(constructor); } } } }
public FieldInitializationReport instantiate() { final AccessibilityChanger changer = new AccessibilityChanger(); Constructor<?> constructor = null; try { constructor = biggestConstructor(field.getType()); changer.enableAccess(constructor); final Object[] args = argResolver.resolveTypeInstances(constructor.getParameterTypes()); Object newFieldInstance = constructor.newInstance(args); new FieldSetter(testClass, field).set(newFieldInstance); return new FieldInitializationReport(field.get(testClass), false, true); } catch (IllegalArgumentException e) { throw new MockitoException("internal error : argResolver provided incorrect types for constructor " + constructor + " of type " + field.getType().getSimpleName(), e); } catch (InvocationTargetException e) { throw new MockitoException("the constructor of type '" + field.getType().getSimpleName() + "' has raised an exception (see the stack trace for cause): " + e.getTargetException().toString(), e); } catch (InstantiationException e) { throw new MockitoException("InstantiationException (see the stack trace for cause): " + e.toString(), e); } catch (IllegalAccessException e) { throw new MockitoException("IllegalAccessException (see the stack trace for cause): " + e.toString(), e); } finally { if(constructor != null) { changer.safelyDisableAccess(constructor); } } }
private FieldInitializationReport acquireFieldInstance() throws IllegalAccessException { Object fieldInstance = field.get(fieldOwner); if(fieldInstance != null) { return new FieldInitializationReport(fieldInstance, false); } instantiator.instantiate(); return new FieldInitializationReport(field.get(fieldOwner), true); }