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


Java TestContext.getTestInstance方法代码示例

本文整理汇总了Java中org.springframework.test.context.TestContext.getTestInstance方法的典型用法代码示例。如果您正苦于以下问题:Java TestContext.getTestInstance方法的具体用法?Java TestContext.getTestInstance怎么用?Java TestContext.getTestInstance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.springframework.test.context.TestContext的用法示例。


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

示例1: beforeTestMethod

import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
	final Object testInstance = testContext.getTestInstance();
	if (!(testInstance instanceof Specification)) {
		return;
	}

	final Specification specification = (Specification) testInstance;
	final List<Object> mocks = new LinkedList<>();
	final ApplicationContext applicationContext = testContext.getApplicationContext();
	final DoubleRegistry doubleRegistry = applicationContext.getBean(DoubleRegistry.BEAN_NAME, DoubleRegistry.class);

	for (DoubleDefinition doubleDefinition : doubleRegistry.doublesSearch()) {
		final Optional<Object> doubleBean = tryToGetBean(applicationContext, doubleDefinition);

		doubleBean.ifPresent(bean -> {
			mocks.add(bean);
			mockUtil.attachMock(bean, specification);
		});
	}

	testContext.setAttribute(MOCKED_BEANS_NAMES, mocks);
}
 
开发者ID:pchudzik,项目名称:springmock,代码行数:24,代码来源:MockAttachingTestExecutionListener.java

示例2: injectDependencies

import org.springframework.test.context.TestContext; //导入方法依赖的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: afterTestMethod

import org.springframework.test.context.TestContext; //导入方法依赖的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

示例4: injectDependencies

import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
/**
 * 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);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:20,代码来源:DependencyInjectionTestExecutionListener.java


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