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

The following examples show how to use org.springframework.test.context.TestContext#setAttribute() . 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: ServletTestExecutionListener.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void setUpRequestContextIfNecessary(TestContext testContext) {
	if (!isActivated(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);
		}
	}
}
 
Example 2
Source File: ServletTestExecutionListener.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void setUpRequestContextIfNecessary(TestContext testContext) {
	if (!isActivated(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);
		}
	}
}
 
Example 3
Source File: DBRiderTestExecutionListener.java    From database-rider with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
    RiderTestContext riderTestContext = SpringRiderTestContext.create(testContext);
    testContext.setAttribute(RIDER_TEST_CONTEXT, riderTestContext);

    RiderRunner riderRunner = new RiderRunner();
    riderRunner.setup(riderTestContext);
    riderRunner.runBeforeTest(riderTestContext);
}
 
Example 4
Source File: ServletTestExecutionListener.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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);
		}
	}
}
 
Example 5
Source File: ServletTestExecutionListener.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * 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);
}
 
Example 6
Source File: ServletTestExecutionListener.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * 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);
}
 
Example 7
Source File: ServletTestExecutionListener.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * 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);
}
 
Example 8
Source File: AbstractDirtiesContextTestExecutionListener.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * 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, @Nullable HierarchyMode hierarchyMode) {
	testContext.markApplicationContextDirty(hierarchyMode);
	testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE);
}
 
Example 9
Source File: AbstractDirtiesContextTestExecutionListener.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * 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, @Nullable HierarchyMode hierarchyMode) {
	testContext.markApplicationContextDirty(hierarchyMode);
	testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE);
}
 
Example 10
Source File: AbstractDirtiesContextTestExecutionListener.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example 11
Source File: ReloadContextTestExecutionListener.java    From geomajas-project-server with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * Marks the {@link ApplicationContext application context} of the supplied {@link TestContext test context} as
 * {@link TestContext#markApplicationContextDirty() dirty}, and sets the
 * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE REINJECT_DEPENDENCIES_ATTRIBUTE}
 * in the test context to <code>true</code> .
 */
protected void reloadContext(TestContext testContext) {
	testContext.markApplicationContextDirty();
	testContext
			.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE);
}