Java 类org.mockito.InjectMocks 实例源码

项目:annotated-mvp    文件:PresenterUnitTestCase.java   
private void findAndSetFields() throws IllegalAccessException, NoSuchFieldException
{
    Field[] fields = getClass().getDeclaredFields();
    for (Field field : fields)
    {
        if (field.getAnnotation(InjectMocks.class) != null){
            field.setAccessible(true);
            Object obj = field.get(this);
            if (MvpPresenter.class.isAssignableFrom(obj.getClass())){
                findAndSetField(obj, "executorService", new RoboExecutorService());
                findAndSetField(obj, "handler", new Handler(Looper.myLooper()));
                findAndSetField(obj, "eventBus", eventBus);
            }
        }
    }
}
项目:wisp    文件:UnitTestDependencyInjectionTestExecutionListener.java   
@Override
protected void injectDependencies(TestContext testContext) throws Exception {
    super.injectDependencies(testContext);
    /**
     * 获取测试类 & fields
     */
    Object bean = testContext.getTestInstance();
    List<Field> fields = getDeclaredFields(bean);
    if (CollectionUtils.isEmpty(fields)) {
        return;
    }
    /**
     * 如果测试类中, 被测试对象含有mockito的@InjectMocks注解,且 被测试对象被事务拦截器拦截 则 用原始对象代替
     */
    for (Field field : fields) {
        InjectMocks injectMocks = field.getAnnotation(InjectMocks.class);
        if (injectMocks == null) {
            continue;
        }
        field.setAccessible(true);
        Object proxy = field.get(bean);
        if (AopUtils.isAopProxy(proxy)) {
            // 替换对象
            Object target = ((Advised) proxy).getTargetSource().getTarget();
            field.set(bean, target);
        }
    }
}
项目:DependencyInjector    文件:DelayedInjectionRunnerValidator.java   
@Override
public void testFinished(Description description) throws Exception {
    try {
        Mockito.validateMockitoUsage();
        if (!testClass.getAnnotatedFields(InjectMocks.class).isEmpty()) {
            throw new IllegalStateException("Do not use @InjectMocks with the DelayedInjectionRunner:"
                + " use @InjectDelayed or change runner");
        }
    } catch (Exception e) {
        notifier.fireTestFailure(new Failure(description, e));
    }
}
项目:jira-dvcs-connector    文件:MockitoTestNgListener.java   
private void resetMocks(Object instance)
{
    Iterable<InstanceField> toReset = Fields.allDeclaredFieldsOf(instance)
            .filter(annotatedBy(Mock.class, InjectMocks.class, Spy.class))
            .notNull()
            .instanceFields();
    for (InstanceField field : toReset)
    {
        field.set(null);
    }
}
项目:astor    文件:MockInjectionTest.java   
@Test
public void shouldReportNicely() throws Exception {
    Object failing = new Object() {
        @InjectMocks
        ThrowingConstructor c;
    };
    try {
        MockitoAnnotations.initMocks(failing);
        fail();
    } catch (MockitoException e) {
        assertContains("correct usage of @InjectMocks", e.getMessage());
    }
}
项目:astor    文件:FieldInitializerTest.java   
@Test(expected = MockitoException.class)
public void shouldFailForLocalTypeField() throws Exception {
    // when
    class LocalType { };

    class TheTestWithLocalType {
        @InjectMocks LocalType field;
    }

    TheTestWithLocalType testWithLocalType = new TheTestWithLocalType();

    // when
    new FieldInitializer(testWithLocalType, testWithLocalType.getClass().getDeclaredField("field"));
}
项目:astor    文件:FieldInitializerTest.java   
public void shouldNotFailIfLocalTypeFieldIsInstantiated() throws Exception {
    // when
    class LocalType { };

    class TheTestWithLocalType {
        @InjectMocks LocalType field = new LocalType();
    }

    TheTestWithLocalType testWithLocalType = new TheTestWithLocalType();

    // when
    new FieldInitializer(testWithLocalType, testWithLocalType.getClass().getDeclaredField("field"));
}
项目:astor    文件:InjectMocksScanner.java   
/**
 * Scan fields annotated by &#064;InjectMocks
 *
 * @return Fields that depends on Mock
 */
private Set<Field> scan() {
    Set<Field> mockDependentFields = new HashSet<Field>();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        if (null != field.getAnnotation(InjectMocks.class)) {
            assertNoAnnotations(field, Mock.class, MockitoAnnotations.Mock.class, Captor.class);
            mockDependentFields.add(field);
        }
    }

    return mockDependentFields;
}
项目:astor    文件:InjectMocksScanner.java   
void assertNoAnnotations(final Field field, final Class... annotations) {
    for (Class annotation : annotations) {
        if (field.isAnnotationPresent(annotation)) {
            new Reporter().unsupportedCombinationOfAnnotations(annotation.getSimpleName(), InjectMocks.class.getSimpleName());
        }
    }
}
项目:astor    文件:MockInjectionUsingSetterOrPropertyTest.java   
@Test
public void shouldReportNicely() throws Exception {
    Object failing = new Object() {
        @InjectMocks ThrowingConstructor failingConstructor;
    };
    try {
        MockitoAnnotations.initMocks(failing);
        fail();
    } catch (MockitoException e) {
        Assertions.assertThat(e.getMessage()).contains("failingConstructor").contains("constructor").contains("threw an exception");
        Assertions.assertThat(e.getCause()).isInstanceOf(RuntimeException.class);
    }
}
项目:astor    文件:FieldInitializerTest.java   
@Test(expected = MockitoException.class)
public void should_fail_for_local_type_field() throws Exception {
    // when
    class LocalType { }

    class TheTestWithLocalType {
        @InjectMocks LocalType field;
    }

    TheTestWithLocalType testWithLocalType = new TheTestWithLocalType();

    // when
    new FieldInitializer(testWithLocalType, testWithLocalType.getClass().getDeclaredField("field"));
}
项目:astor    文件:FieldInitializerTest.java   
@Test
public void should_not_fail_if_local_type_field_is_instantiated() throws Exception {
    // when
    class LocalType { }

    class TheTestWithLocalType {
        @InjectMocks LocalType field = new LocalType();
    }

    TheTestWithLocalType testWithLocalType = new TheTestWithLocalType();

    // when
    new FieldInitializer(testWithLocalType, testWithLocalType.getClass().getDeclaredField("field"));
}
项目:wisp    文件:UnitTestDependencyInjectionTestExecutionListener.java   
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
    super.afterTestMethod(testContext);

    DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) testContext.getApplicationContext()
            .getAutowireCapableBeanFactory();

    /**
     * 方法结束后记录被测试对象的bean名称
     */
    Object bean = testContext.getTestInstance();
    List<Field> fields = getDeclaredFields(bean);
    for (Field field : fields) {
        InjectMocks injectMocks = field.getAnnotation(InjectMocks.class);
        if (injectMocks == null) {
            continue;
        }
        Object testedBean = null;
        String testedBeanName = null;
        /**
         * 被测试的对象如果通过spring自动注入,则记录
         * 两种注入方式 Autowired
         * Resource
         */
        if (field.getAnnotation(Autowired.class) != null) {
            Qualifier qualifier = field.getAnnotation(Qualifier.class);
            testedBean = qualifier == null ? beanFactory.getBean(field.getType())
                    : beanFactory.getBean(qualifier.value());
            testedBeanName = qualifier == null ? beanFactory.getBeanNamesForType(field.getType())[0]
                    : qualifier.value();
        } else if (field.getAnnotation(Resource.class) != null) {
            Resource resource = field.getAnnotation(Resource.class);
            Class<?> type = resource.type();
            String name = resource.name();
            if (StringUtils.isNotEmpty(name)) {
                testedBean = beanFactory.getBean(name);
                testedBeanName = name;
            } else {
                testedBean = (type != Object.class) ? beanFactory.getBean(type)
                        : beanFactory.getBean(field.getType());
                testedBeanName = (type != Object.class) ? beanFactory.getBeanNamesForType(type)[0]
                        : beanFactory.getBeanNamesForType(field.getType())[0];
            }
        }

        if (testedBean != null) {
            testedObjects.put(testedBeanName, testedBean);
        }
    }
}
项目:DependencyInjector    文件:AnnotationResolver.java   
public AnnotationResolver(TestClass testClass, Object target) {
    this(testClass, target, Inject.class, InjectMocks.class, Mock.class, Spy.class, InjectDelayed.class);
}
项目:astor    文件:WrongSetOfAnnotationsTest.java   
@Test(expected=MockitoException.class)
public void shouldNotAllowSpyAndInjectMock() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @InjectMocks @Spy List mock;
    });
}
项目:astor    文件:WrongSetOfAnnotationsTest.java   
@Test(expected=MockitoException.class)
public void shouldNotAllowMockAndInjectMock() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @InjectMocks @Mock List mock;
    });
}
项目:astor    文件:WrongSetOfAnnotationsTest.java   
@Test(expected=MockitoException.class)
public void shouldNotAllowCaptorAndInjectMock() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @InjectMocks @Captor ArgumentCaptor captor;
    });
}
项目:astor    文件:WrongSetOfAnnotationsTest.java   
@Test(expected=MockitoException.class)
public void shouldNotAllowSpyAndInjectMock() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @InjectMocks @Spy List mock;
    });
}
项目:astor    文件:WrongSetOfAnnotationsTest.java   
@Test(expected=MockitoException.class)
public void shouldNotAllowMockAndInjectMock() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @InjectMocks @Mock List mock;
    });
}
项目:astor    文件:WrongSetOfAnnotationsTest.java   
@Test(expected=MockitoException.class)
public void shouldNotAllowCaptorAndInjectMock() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @InjectMocks @Captor ArgumentCaptor captor;
    });
}
项目:miracle-framework    文件:InjectUtil.java   
private static Collection<Field> getInjectMocksFields(final Class<?> clazz) {
    return getFieldsByAnnotation(clazz, InjectMocks.class);
}