diff --git a/spring-test/src/main/java/org/springframework/test/context/web/socket/MockServerContainerContextCustomizerFactory.java b/spring-test/src/main/java/org/springframework/test/context/web/socket/MockServerContainerContextCustomizerFactory.java index e69b2bab072..cdbf6f36acf 100644 --- a/spring-test/src/main/java/org/springframework/test/context/web/socket/MockServerContainerContextCustomizerFactory.java +++ b/spring-test/src/main/java/org/springframework/test/context/web/socket/MockServerContainerContextCustomizerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,26 +19,25 @@ package org.springframework.test.context.web.socket; import java.util.List; import org.springframework.beans.BeanUtils; -import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.lang.Nullable; import org.springframework.test.context.ContextConfigurationAttributes; import org.springframework.test.context.ContextCustomizer; import org.springframework.test.context.ContextCustomizerFactory; +import org.springframework.test.context.TestContextAnnotationUtils; +import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.util.ClassUtils; /** * {@link ContextCustomizerFactory} which creates a {@link MockServerContainerContextCustomizer} - * if WebSocket support is present in the classpath and the test class is annotated - * with {@code @WebAppConfiguration}. + * if WebSocket support is present in the classpath and the test class or one of + * its enclosing classes is annotated or meta-annotated with + * {@link WebAppConfiguration @WebAppConfiguration}. * * @author Sam Brannen * @since 4.3.1 */ class MockServerContainerContextCustomizerFactory implements ContextCustomizerFactory { - private static final String WEB_APP_CONFIGURATION_ANNOTATION_CLASS_NAME = - "org.springframework.test.context.web.WebAppConfiguration"; - private static final String MOCK_SERVER_CONTAINER_CONTEXT_CUSTOMIZER_CLASS_NAME = "org.springframework.test.context.web.socket.MockServerContainerContextCustomizer"; @@ -68,8 +67,7 @@ class MockServerContainerContextCustomizerFactory implements ContextCustomizerFa } private static boolean isAnnotatedWithWebAppConfiguration(Class testClass) { - return (AnnotatedElementUtils.findMergedAnnotationAttributes(testClass, - WEB_APP_CONFIGURATION_ANNOTATION_CLASS_NAME, false, false) != null); + return TestContextAnnotationUtils.hasAnnotation(testClass, WebAppConfiguration.class); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/web/socket/WebSocketServletServerContainerFactoryBeanTests.java b/spring-test/src/test/java/org/springframework/test/context/web/socket/WebSocketServletServerContainerFactoryBeanTests.java index 42fecb9d3af..1cd89ea6f8b 100644 --- a/spring-test/src/test/java/org/springframework/test/context/web/socket/WebSocketServletServerContainerFactoryBeanTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/web/socket/WebSocketServletServerContainerFactoryBeanTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package org.springframework.test.context.web.socket; import javax.websocket.server.ServerContainer; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -46,6 +47,20 @@ class WebSocketServletServerContainerFactoryBeanTests { assertThat(serverContainer.getDefaultMaxTextMessageBufferSize()).isEqualTo(42); } + /* + * @Nested test class to verify that the MockServerContainerContextCustomizerFactory + * properly supports finding @WebAppConfiguration on an enclosing class. + */ + @Nested + class NestedTests { + + @Test // gh-29037 + void servletServerContainerFactoryBeanSupport(@Autowired ServerContainer serverContainer) { + assertThat(serverContainer.getDefaultMaxTextMessageBufferSize()).isEqualTo(42); + } + + } + @Configuration @EnableWebSocket