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


Java TestContext.removeAttribute方法代码示例

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


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

示例1: afterTestMethod

import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
/**
 * If the {@link #RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} in the supplied
 * {@code TestContext} has a value of {@link Boolean#TRUE}, this method will
 * (1) clean up thread-local state after each test method by {@linkplain
 * RequestContextHolder#resetRequestAttributes() resetting} Spring Web's
 * {@code RequestContextHolder} and (2) ensure that new mocks are injected
 * into the test instance for subsequent tests by setting the
 * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE}
 * in the test context to {@code true}.
 *
 * <p>The {@link #RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} and
 * {@link #POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} will be subsequently
 * removed from the test context, regardless of their values.
 *
 * @see TestExecutionListener#afterTestMethod(TestContext)
 */
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
	if (Boolean.TRUE.equals(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE))) {
		if (logger.isDebugEnabled()) {
			logger.debug(String.format("Resetting RequestContextHolder for test context %s.", testContext));
		}
		RequestContextHolder.resetRequestAttributes();
		testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE,
			Boolean.TRUE);
	}
	testContext.removeAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
	testContext.removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:30,代码来源:ServletTestExecutionListener.java

示例2: 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.removeAttribute方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。