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


Java TestContext.setAttribute方法代码示例

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


在下文中一共展示了TestContext.setAttribute方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: createTestContextManager

import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
/**
 * Creates a new {@link TestContextManager} for the supplied test class.
 * <p>
 * Can be overridden by subclasses.
 *
 * @param clazz
 *            the test class to be managed
 */
@Override
protected TestContextManager createTestContextManager(final Class<?> clazz) {
    return new TestContextManager(clazz) {
        @Override
        public void beforeTestClass() throws Exception {
            super.beforeTestClass();
        }

        @Override
        public void afterTestClass() throws Exception {
            // If we aren't caching the Spring context them mark it dirty so
            // it is destroyed.
            if (!cacheSpringContext) {
                final TestContext testContext = getTestContext();
                testContext.markApplicationContextDirty(HierarchyMode.EXHAUSTIVE);
                testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE,
                        Boolean.TRUE);
            }

            super.afterTestClass();
        }
    };
}
 
开发者ID:gchq,项目名称:stroom-proxy,代码行数:32,代码来源:StroomSpringJUnit4ClassRunner.java

示例3: setUpRequestContextIfNecessary

import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
private void setUpRequestContextIfNecessary(TestContext testContext) {
	if (notAnnotatedWithWebAppConfiguration(testContext) || alreadyPopulatedRequestContextHolder(testContext)) {
		return;
	}

	ApplicationContext context = testContext.getApplicationContext();

	if (context instanceof WebApplicationContext) {
		WebApplicationContext wac = (WebApplicationContext) context;
		ServletContext servletContext = wac.getServletContext();
		Assert.state(servletContext instanceof MockServletContext, String.format(
			"The WebApplicationContext for test context %s must be configured with a MockServletContext.",
			testContext));

		if (logger.isDebugEnabled()) {
			logger.debug(String.format(
				"Setting up MockHttpServletRequest, MockHttpServletResponse, ServletWebRequest, and RequestContextHolder for test context %s.",
				testContext));
		}

		MockServletContext mockServletContext = (MockServletContext) servletContext;
		MockHttpServletRequest request = new MockHttpServletRequest(mockServletContext);
		request.setAttribute(CREATED_BY_THE_TESTCONTEXT_FRAMEWORK, Boolean.TRUE);
		MockHttpServletResponse response = new MockHttpServletResponse();
		ServletWebRequest servletWebRequest = new ServletWebRequest(request, response);

		RequestContextHolder.setRequestAttributes(servletWebRequest);
		testContext.setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
		testContext.setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);

		if (wac instanceof ConfigurableApplicationContext) {
			@SuppressWarnings("resource")
			ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) wac;
			ConfigurableListableBeanFactory bf = configurableApplicationContext.getBeanFactory();
			bf.registerResolvableDependency(MockHttpServletResponse.class, response);
			bf.registerResolvableDependency(ServletWebRequest.class, servletWebRequest);
		}
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:40,代码来源:ServletTestExecutionListener.java

示例4: buildTestContext

import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
@Override
public TestContext buildTestContext() {
	TestContext context = super.buildTestContext();
	WebEnvironment webEnvironment = getWebEnvironment(context.getTestClass());
	if (webEnvironment == WebEnvironment.MOCK && hasWebEnvironmentClasses()) {
		context.setAttribute(ACTIVATE_SERVLET_LISTENER, true);
	}
	else if (webEnvironment != null && webEnvironment.isEmbedded()) {
		context.setAttribute(ACTIVATE_SERVLET_LISTENER, false);
	}
	return context;
}
 
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:13,代码来源:SpringBootTestContextBootstrapper.java

示例5: 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

示例6: dirtyContext

import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
/**
 * Mark the {@linkplain ApplicationContext application context} of the supplied
 * {@linkplain TestContext test context} as
 * {@linkplain TestContext#markApplicationContextDirty(DirtiesContext.HierarchyMode) dirty}
 * and set {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE
 * REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context to {@code true}.
 * @param testContext the test context whose application context should
 * be marked as dirty
 * @param hierarchyMode the context cache clearing mode to be applied if the
 * context is part of a hierarchy; may be {@code null}
 * @since 3.2.2
 */
protected void dirtyContext(TestContext testContext, HierarchyMode hierarchyMode) {
	testContext.markApplicationContextDirty(hierarchyMode);
	testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:17,代码来源:AbstractDirtiesContextTestExecutionListener.java


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