Skip to content

Commit 2c75eb8

Browse files
committed
Support @nested tests in MockServerContainerContextCustomizerFactory
Prior to this commit, MockServerContainerContextCustomizerFactory did not find @WebAppConfiguration on an enclosing class and therefore failed to create a MockServerContainerContextCustomizer for a @nested test class. This commit addresses this by using TestContextAnnotationUtils to determine if the test class is "annotated" with @WebAppConfiguration. Closes gh-29037
1 parent 711820e commit 2c75eb8

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

spring-test/src/main/java/org/springframework/test/context/web/socket/MockServerContainerContextCustomizerFactory.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,26 +19,25 @@
1919
import java.util.List;
2020

2121
import org.springframework.beans.BeanUtils;
22-
import org.springframework.core.annotation.AnnotatedElementUtils;
2322
import org.springframework.lang.Nullable;
2423
import org.springframework.test.context.ContextConfigurationAttributes;
2524
import org.springframework.test.context.ContextCustomizer;
2625
import org.springframework.test.context.ContextCustomizerFactory;
26+
import org.springframework.test.context.TestContextAnnotationUtils;
27+
import org.springframework.test.context.web.WebAppConfiguration;
2728
import org.springframework.util.ClassUtils;
2829

2930
/**
3031
* {@link ContextCustomizerFactory} which creates a {@link MockServerContainerContextCustomizer}
31-
* if WebSocket support is present in the classpath and the test class is annotated
32-
* with {@code @WebAppConfiguration}.
32+
* if WebSocket support is present in the classpath and the test class or one of
33+
* its enclosing classes is annotated or meta-annotated with
34+
* {@link WebAppConfiguration @WebAppConfiguration}.
3335
*
3436
* @author Sam Brannen
3537
* @since 4.3.1
3638
*/
3739
class MockServerContainerContextCustomizerFactory implements ContextCustomizerFactory {
3840

39-
private static final String WEB_APP_CONFIGURATION_ANNOTATION_CLASS_NAME =
40-
"org.springframework.test.context.web.WebAppConfiguration";
41-
4241
private static final String MOCK_SERVER_CONTAINER_CONTEXT_CUSTOMIZER_CLASS_NAME =
4342
"org.springframework.test.context.web.socket.MockServerContainerContextCustomizer";
4443

@@ -68,8 +67,7 @@ public ContextCustomizer createContextCustomizer(Class<?> testClass,
6867
}
6968

7069
private static boolean isAnnotatedWithWebAppConfiguration(Class<?> testClass) {
71-
return (AnnotatedElementUtils.findMergedAnnotationAttributes(testClass,
72-
WEB_APP_CONFIGURATION_ANNOTATION_CLASS_NAME, false, false) != null);
70+
return TestContextAnnotationUtils.hasAnnotation(testClass, WebAppConfiguration.class);
7371
}
7472

7573
}

spring-test/src/test/java/org/springframework/test/context/web/socket/WebSocketServletServerContainerFactoryBeanTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import javax.websocket.server.ServerContainer;
2020

21+
import org.junit.jupiter.api.Nested;
2122
import org.junit.jupiter.api.Test;
2223

2324
import org.springframework.beans.factory.annotation.Autowired;
@@ -46,6 +47,20 @@ void servletServerContainerFactoryBeanSupport(@Autowired ServerContainer serverC
4647
assertThat(serverContainer.getDefaultMaxTextMessageBufferSize()).isEqualTo(42);
4748
}
4849

50+
/*
51+
* @Nested test class to verify that the MockServerContainerContextCustomizerFactory
52+
* properly supports finding @WebAppConfiguration on an enclosing class.
53+
*/
54+
@Nested
55+
class NestedTests {
56+
57+
@Test // gh-29037
58+
void servletServerContainerFactoryBeanSupport(@Autowired ServerContainer serverContainer) {
59+
assertThat(serverContainer.getDefaultMaxTextMessageBufferSize()).isEqualTo(42);
60+
}
61+
62+
}
63+
4964

5065
@Configuration
5166
@EnableWebSocket

0 commit comments

Comments
 (0)