Skip to content

Commit

Permalink
Fix bug in @WebAppConfiguration support
Browse files Browse the repository at this point in the history
Use MetaAnnotationUtils.findMergedAnnotation() instead of the
MergedAnnotations API in order to provide proper support for
@NestedTestConfiguration.

See gh-19930
  • Loading branch information
sbrannen committed Oct 9, 2020
1 parent 07027b8 commit d9507a0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package org.springframework.test.context.web;

import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.RepeatableContainers;
import org.springframework.test.context.ContextLoader;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContextBootstrapper;
Expand Down Expand Up @@ -48,7 +45,7 @@ public class WebTestContextBootstrapper extends DefaultTestContextBootstrapper {
*/
@Override
protected Class<? extends ContextLoader> getDefaultContextLoaderClass(Class<?> testClass) {
if (getWebAppConfiguration(testClass).isPresent()) {
if (getWebAppConfiguration(testClass) != null) {
return WebDelegatingSmartContextLoader.class;
}
else {
Expand All @@ -64,18 +61,17 @@ protected Class<? extends ContextLoader> getDefaultContextLoaderClass(Class<?> t
*/
@Override
protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
MergedAnnotation<WebAppConfiguration> webAppConfiguration = getWebAppConfiguration(mergedConfig.getTestClass());
if (webAppConfiguration.isPresent()) {
return new WebMergedContextConfiguration(mergedConfig, webAppConfiguration.getString("value"));
WebAppConfiguration webAppConfiguration = getWebAppConfiguration(mergedConfig.getTestClass());
if (webAppConfiguration != null) {
return new WebMergedContextConfiguration(mergedConfig, webAppConfiguration.value());
}
else {
return mergedConfig;
}
}

private static MergedAnnotation<WebAppConfiguration> getWebAppConfiguration(Class<?> testClass) {
return MergedAnnotations.from(testClass, MetaAnnotationUtils.getSearchStrategy(testClass),
RepeatableContainers.none()).get(WebAppConfiguration.class);
private static WebAppConfiguration getWebAppConfiguration(Class<?> testClass) {
return MetaAnnotationUtils.findMergedAnnotation(testClass, WebAppConfiguration.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@
* @see ConstructorInjectionNestedTests
* @see org.springframework.test.context.junit4.nested.NestedTestsWithSpringRulesTests
*/
@SpringJUnitConfig(Config.class)
@SpringJUnitWebConfig(Config.class)
class WebAppConfigurationNestedTests {

@Test
void test(ApplicationContext context) {
assertThat(context).isNotInstanceOf(WebApplicationContext.class);
assertThat(context).isInstanceOf(WebApplicationContext.class);
}


Expand Down Expand Up @@ -78,18 +78,27 @@ class NestedWithInheritedConfigTests {

@Test
void test(ApplicationContext context) {
assertThat(context).isNotInstanceOf(WebApplicationContext.class);
assertThat(context).isInstanceOf(WebApplicationContext.class);
}


@Nested
class DoubleNestedWithImplicitlyInheritedConfigWebTests {

@Test
void test(ApplicationContext context) {
assertThat(context).isInstanceOf(WebApplicationContext.class);
}
}

@Nested
@NestedTestConfiguration(OVERRIDE)
@SpringJUnitWebConfig(Config.class)
@SpringJUnitConfig(Config.class)
class DoubleNestedWithOverriddenConfigWebTests {

@Test
void test(ApplicationContext context) {
assertThat(context).isInstanceOf(WebApplicationContext.class);
assertThat(context).isNotInstanceOf(WebApplicationContext.class);
}


Expand All @@ -99,18 +108,18 @@ class TripleNestedWithInheritedConfigWebTests {

@Test
void test(ApplicationContext context) {
assertThat(context).isInstanceOf(WebApplicationContext.class);
assertThat(context).isNotInstanceOf(WebApplicationContext.class);
}
}
}

@Nested
@NestedTestConfiguration(INHERIT)
class DoubleNestedWithInheritedConfigAndTestInterfaceTests implements TestInterface {
@Nested
@NestedTestConfiguration(INHERIT)
class TripleNestedWithInheritedConfigAndTestInterfaceTests implements TestInterface {

@Test
void test(ApplicationContext context) {
assertThat(context).isInstanceOf(WebApplicationContext.class);
@Test
void test(ApplicationContext context) {
assertThat(context).isInstanceOf(WebApplicationContext.class);
}
}
}
}
Expand Down

0 comments on commit d9507a0

Please sign in to comment.