當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。