/** * Sets up thread-local state before each test method via Spring Web's * {@link RequestContextHolder}, but only if the * {@linkplain TestContext#getTestClass() test class} is annotated with * {@link WebAppConfiguration @WebAppConfiguration}. * @see TestExecutionListener#beforeTestMethod(TestContext) * @see #setUpRequestContextIfNecessary(TestContext) */ @Override public void beforeTestMethod(TestContext testContext) throws Exception { setUpRequestContextIfNecessary(testContext); }
/** * Returns {@code "-context.xml" and "Context.groovy"} in order to * support detection of a default XML config file or Groovy script. */ @Override protected String[] getResourceSuffixes() { return new String[] { super.getResourceSuffix(), "Context.groovy" }; }
@Override protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) { return new WebMergedContextConfiguration(mergedConfig, "src/test/webapp"); } }
@WebAppConfiguration static class AtWebAppConfigWebTestCase { }
@Test public void configMustNotContainAnnotatedClasses() throws Exception { expectedException.expect(IllegalStateException.class); expectedException.expectMessage(containsString("does not support annotated classes")); GenericXmlWebContextLoader loader = new GenericXmlWebContextLoader(); WebMergedContextConfiguration mergedConfig = new WebMergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY, new Class<?>[] { getClass() }, null, EMPTY_STRING_ARRAY, EMPTY_STRING_ARRAY, EMPTY_STRING_ARRAY, "resource/path", loader, null, null); loader.loadContext(mergedConfig); }
@Test public void configMustNotContainLocations() throws Exception { expectedException.expect(IllegalStateException.class); expectedException.expectMessage(containsString("does not support resource locations")); AnnotationConfigWebContextLoader loader = new AnnotationConfigWebContextLoader(); WebMergedContextConfiguration mergedConfig = new WebMergedContextConfiguration(getClass(), new String[] { "config.xml" }, EMPTY_CLASS_ARRAY, null, EMPTY_STRING_ARRAY, EMPTY_STRING_ARRAY, EMPTY_STRING_ARRAY, "resource/path", loader, null, null); loader.loadContext(mergedConfig); }
/** * Returns a {@link WebMergedContextConfiguration} if the test class in the * supplied {@code MergedContextConfiguration} is annotated with * {@link WebAppConfiguration @WebAppConfiguration} and otherwise returns * the supplied instance unmodified. */ @Override protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) { WebAppConfiguration webAppConfiguration = AnnotatedElementUtils.findMergedAnnotation(mergedConfig.getTestClass(), WebAppConfiguration.class); if (webAppConfiguration != null) { return new WebMergedContextConfiguration(mergedConfig, webAppConfiguration.value()); } else { return mergedConfig; } }
@Test public void atWebAppConfigTestCaseWithPresetRequestAttributes() throws Exception { BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(AtWebAppConfigWebTestCase.class); listener.beforeTestClass(testContext); assertRequestAttributesExist(); assertWebAppConfigTestCase(); }
@Test public void atWebAppConfigTestCaseWithoutExistingRequestAttributes() throws Exception { BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(AtWebAppConfigWebTestCase.class); RequestContextHolder.resetRequestAttributes(); listener.beforeTestClass(testContext); assertRequestAttributesDoNotExist(); assertWebAppConfigTestCase(); }
/** * Load bean definitions into the supplied {@link GenericWebApplicationContext context} * from the locations in the supplied {@code WebMergedContextConfiguration}, using an * {@link XmlBeanDefinitionReader}. * @see AbstractGenericWebContextLoader#loadBeanDefinitions */ @Override protected void loadBeanDefinitions(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig) { new XmlBeanDefinitionReader(context).loadBeanDefinitions(webMergedConfig.getLocations()); }
/** * Verifies bug fix for <a href="https://jira.spring.io/browse/SPR-11626">SPR-11626</a>. * * @see #ensureMocksAreReinjectedBetweenTests_1 */ @Test public void ensureMocksAreReinjectedBetweenTests_2() { assertInjectedServletRequestEqualsRequestInRequestContextHolder(); }
private void assertSetUpOutsideOfStelAttributeExists() { RequestAttributes requestAttributes = assertRequestAttributesExist(); Object setUpOutsideOfStel = requestAttributes.getAttribute(SET_UP_OUTSIDE_OF_STEL, RequestAttributes.SCOPE_REQUEST); assertNotNull(SET_UP_OUTSIDE_OF_STEL + " should exist as a request attribute", setUpOutsideOfStel); }
@Bean public ServletContextAwareBean servletContextAwareBean() { return new ServletContextAwareBean(); } }
@WebAppConfiguration static class WebClassesFoo extends ClassesFoo { }
/** * Sets up thread-local state during the <em>test instance preparation</em> * callback phase via Spring Web's {@link RequestContextHolder}, but only if * the {@linkplain TestContext#getTestClass() test class} is annotated with * {@link WebAppConfiguration @WebAppConfiguration}. * @see TestExecutionListener#prepareTestInstance(TestContext) * @see #setUpRequestContextIfNecessary(TestContext) */ @Override public void prepareTestInstance(TestContext testContext) throws Exception { setUpRequestContextIfNecessary(testContext); }
/** * Load bean definitions into the supplied {@link GenericWebApplicationContext context} * from the locations in the supplied {@code WebMergedContextConfiguration} using a * {@link GroovyBeanDefinitionReader}. * @param context the context into which the bean definitions should be loaded * @param webMergedConfig the merged context configuration * @see AbstractGenericWebContextLoader#loadBeanDefinitions */ @Override protected void loadBeanDefinitions(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig) { new GroovyBeanDefinitionReader(context).loadBeanDefinitions(webMergedConfig.getLocations()); }
/** * Verifies bug fix for <a href="https://jira.spring.io/browse/SPR-11626">SPR-11626</a>. * * @see #ensureMocksAreReinjectedBetweenTests_2 */ @Test public void ensureMocksAreReinjectedBetweenTests_1() { assertInjectedServletRequestEqualsRequestInRequestContextHolder(); }
private void assertSetUpOutsideOfStelAttributeDoesNotExist() { RequestAttributes requestAttributes = assertRequestAttributesExist(); Object setUpOutsideOfStel = requestAttributes.getAttribute(SET_UP_OUTSIDE_OF_STEL, RequestAttributes.SCOPE_REQUEST); assertNull(SET_UP_OUTSIDE_OF_STEL + " should NOT exist as a request attribute", setUpOutsideOfStel); }
@WebAppConfiguration static class WebAppConfigurationAnnotatedClass {}
/** * @author Sam Brannen * @since 4.3 */ @WebAppConfiguration @ContextConfiguration(classes = Config.class) interface WebAppConfigurationTestInterface { @Configuration static class Config { /* no user beans required for these tests */ } }