当前位置: 首页>>代码示例>>Java>>正文


Java InjectMocks类代码示例

本文整理汇总了Java中org.mockito.InjectMocks的典型用法代码示例。如果您正苦于以下问题:Java InjectMocks类的具体用法?Java InjectMocks怎么用?Java InjectMocks使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


InjectMocks类属于org.mockito包,在下文中一共展示了InjectMocks类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: findAndSetFields

import org.mockito.InjectMocks; //导入依赖的package包/类
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);
            }
        }
    }
}
 
开发者ID:aschattney,项目名称:annotated-mvp,代码行数:17,代码来源:PresenterUnitTestCase.java

示例2: injectDependencies

import org.mockito.InjectMocks; //导入依赖的package包/类
@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);
        }
    }
}
 
开发者ID:warlock-china,项目名称:wisp,代码行数:29,代码来源:UnitTestDependencyInjectionTestExecutionListener.java

示例3: testFinished

import org.mockito.InjectMocks; //导入依赖的package包/类
@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));
    }
}
 
开发者ID:ljacqu,项目名称:DependencyInjector,代码行数:13,代码来源:DelayedInjectionRunnerValidator.java

示例4: resetMocks

import org.mockito.InjectMocks; //导入依赖的package包/类
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);
    }
}
 
开发者ID:edgehosting,项目名称:jira-dvcs-connector,代码行数:12,代码来源:MockitoTestNgListener.java

示例5: shouldReportNicely

import org.mockito.InjectMocks; //导入依赖的package包/类
@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());
    }
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:14,代码来源:MockInjectionTest.java

示例6: shouldFailForLocalTypeField

import org.mockito.InjectMocks; //导入依赖的package包/类
@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"));
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:15,代码来源:FieldInitializerTest.java

示例7: shouldNotFailIfLocalTypeFieldIsInstantiated

import org.mockito.InjectMocks; //导入依赖的package包/类
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"));
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:14,代码来源:FieldInitializerTest.java

示例8: scan

import org.mockito.InjectMocks; //导入依赖的package包/类
/**
 * 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;
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:18,代码来源:InjectMocksScanner.java

示例9: assertNoAnnotations

import org.mockito.InjectMocks; //导入依赖的package包/类
void assertNoAnnotations(final Field field, final Class... annotations) {
    for (Class annotation : annotations) {
        if (field.isAnnotationPresent(annotation)) {
            new Reporter().unsupportedCombinationOfAnnotations(annotation.getSimpleName(), InjectMocks.class.getSimpleName());
        }
    }
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:8,代码来源:InjectMocksScanner.java

示例10: shouldReportNicely

import org.mockito.InjectMocks; //导入依赖的package包/类
@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);
    }
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:14,代码来源:MockInjectionUsingSetterOrPropertyTest.java

示例11: should_fail_for_local_type_field

import org.mockito.InjectMocks; //导入依赖的package包/类
@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"));
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:15,代码来源:FieldInitializerTest.java

示例12: should_not_fail_if_local_type_field_is_instantiated

import org.mockito.InjectMocks; //导入依赖的package包/类
@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"));
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:15,代码来源:FieldInitializerTest.java

示例13: afterTestMethod

import org.mockito.InjectMocks; //导入依赖的package包/类
@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);
        }
    }
}
 
开发者ID:warlock-china,项目名称:wisp,代码行数:51,代码来源:UnitTestDependencyInjectionTestExecutionListener.java

示例14: AnnotationResolver

import org.mockito.InjectMocks; //导入依赖的package包/类
public AnnotationResolver(TestClass testClass, Object target) {
    this(testClass, target, Inject.class, InjectMocks.class, Mock.class, Spy.class, InjectDelayed.class);
}
 
开发者ID:ljacqu,项目名称:DependencyInjector,代码行数:4,代码来源:AnnotationResolver.java

示例15: shouldNotAllowSpyAndInjectMock

import org.mockito.InjectMocks; //导入依赖的package包/类
@Test(expected=MockitoException.class)
public void shouldNotAllowSpyAndInjectMock() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @InjectMocks @Spy List mock;
    });
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:7,代码来源:WrongSetOfAnnotationsTest.java


注:本文中的org.mockito.InjectMocks类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。