Java Code Examples for org.springframework.test.context.TestContext#getTestInstance()

The following examples show how to use org.springframework.test.context.TestContext#getTestInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnitTestDependencyInjectionTestExecutionListener.java    From wisp with Apache License 2.0 5 votes vote down vote up
@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);
        }
    }
}
 
Example 2
Source File: UnitTestDependencyInjectionTestExecutionListener.java    From wisp with Apache License 2.0 4 votes vote down vote up
@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);
        }
    }
}
 
Example 3
Source File: DependencyInjectionTestExecutionListener.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Performs dependency injection and bean initialization for the supplied
 * {@link TestContext} as described in
 * {@link #prepareTestInstance(TestContext) prepareTestInstance()}.
 * <p>The {@link #REINJECT_DEPENDENCIES_ATTRIBUTE} will be subsequently removed
 * from the test context, regardless of its value.
 * @param testContext the test context for which dependency injection should
 * be performed (never {@code null})
 * @throws Exception allows any exception to propagate
 * @see #prepareTestInstance(TestContext)
 * @see #beforeTestMethod(TestContext)
 */
protected void injectDependencies(TestContext testContext) throws Exception {
	Object bean = testContext.getTestInstance();
	Class<?> clazz = testContext.getTestClass();
	AutowireCapableBeanFactory beanFactory = testContext.getApplicationContext().getAutowireCapableBeanFactory();
	beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
	beanFactory.initializeBean(bean, clazz.getName() + AutowireCapableBeanFactory.ORIGINAL_INSTANCE_SUFFIX);
	testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE);
}
 
Example 4
Source File: DependencyInjectionTestExecutionListener.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Performs dependency injection and bean initialization for the supplied
 * {@link TestContext} as described in
 * {@link #prepareTestInstance(TestContext) prepareTestInstance()}.
 * <p>The {@link #REINJECT_DEPENDENCIES_ATTRIBUTE} will be subsequently removed
 * from the test context, regardless of its value.
 * @param testContext the test context for which dependency injection should
 * be performed (never {@code null})
 * @throws Exception allows any exception to propagate
 * @see #prepareTestInstance(TestContext)
 * @see #beforeTestMethod(TestContext)
 */
protected void injectDependencies(TestContext testContext) throws Exception {
	Object bean = testContext.getTestInstance();
	Class<?> clazz = testContext.getTestClass();
	AutowireCapableBeanFactory beanFactory = testContext.getApplicationContext().getAutowireCapableBeanFactory();
	beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
	beanFactory.initializeBean(bean, clazz.getName() + AutowireCapableBeanFactory.ORIGINAL_INSTANCE_SUFFIX);
	testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE);
}
 
Example 5
Source File: DependencyInjectionTestExecutionListener.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Performs dependency injection and bean initialization for the supplied
 * {@link TestContext} as described in
 * {@link #prepareTestInstance(TestContext) prepareTestInstance()}.
 * <p>The {@link #REINJECT_DEPENDENCIES_ATTRIBUTE} will be subsequently removed
 * from the test context, regardless of its value.
 * @param testContext the test context for which dependency injection should
 * be performed (never {@code null})
 * @throws Exception allows any exception to propagate
 * @see #prepareTestInstance(TestContext)
 * @see #beforeTestMethod(TestContext)
 */
protected void injectDependencies(final TestContext testContext) throws Exception {
	Object bean = testContext.getTestInstance();
	AutowireCapableBeanFactory beanFactory = testContext.getApplicationContext().getAutowireCapableBeanFactory();
	beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
	beanFactory.initializeBean(bean, testContext.getTestClass().getName());
	testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE);
}