/** * @param testInstance the test instance. * @return all the values of the fields of the test instance annotated with {@link Mock} or {@link Spy}. */ @SuppressWarnings("unchecked") private Collection<Object> instanceMocksOf(Object testInstance) { return Fields.allDeclaredFieldsOf(testInstance) .filter(annotatedBy(Mock.class, Spy.class)) .notNull() .assignedValues(); } }
@SuppressWarnings({"deprecation", "unchecked"}) private Collection<Object> instanceMocksOf(Object instance) { return Fields.allDeclaredFieldsOf(instance) .filter(annotatedBy(Mock.class, Spy.class, MockitoAnnotations.Mock.class)) .notNull() .assignedValues(); }
private void initializeCaptors(Object instance) { List<InstanceField> instanceFields = Fields.allDeclaredFieldsOf(instance).filter(annotatedBy(Captor.class)).instanceFields(); for (InstanceField instanceField : instanceFields) { instanceField.set(new CaptorAnnotationProcessor().process(instanceField.annotation(Captor.class), instanceField.jdkField())); } }
/** * Reset all the arguments captors of the test instance. * * @param testMethod the invoked test method. * @param testResult metadata about the class under test. */ private void resetCaptors(IInvokedMethod testMethod, ITestResult testResult) { if (!testMethod.isConfigurationMethod()) { @SuppressWarnings("unchecked") List<InstanceField> instanceFields = Fields .allDeclaredFieldsOf(testResult.getInstance()) .filter(annotatedBy(Captor.class)) .instanceFields(); for (InstanceField instanceField : instanceFields) { Captor captor = instanceField.annotation(Captor.class); Field field = instanceField.jdkField(); instanceField.set(new CaptorAnnotationProcessor().process(captor, field)); } } }