Polish tests to make use of @ParamterizedTest

Update tests to use @ParamterizedTest to reduce duplication.
This commit is contained in:
Andy Wilkinson 2022-11-05 18:45:00 -07:00 committed by Phillip Webb
parent d7941c6315
commit bf468ab808
4 changed files with 77 additions and 121 deletions

View File

@ -267,3 +267,7 @@ tasks.named("checkSpringConfigurationMetadata").configure {
"spring.groovy.template.configuration.*" "spring.groovy.template.configuration.*"
] ]
} }
test {
jvmArgs += "--add-opens=java.base/java.net=ALL-UNNAMED"
}

View File

@ -17,14 +17,20 @@
package org.springframework.boot.autoconfigure.web.servlet; package org.springframework.boot.autoconfigure.web.servlet;
import java.net.URI; import java.net.URI;
import java.util.stream.Stream;
import jakarta.servlet.MultipartConfigElement; import jakarta.servlet.MultipartConfigElement;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.testsupport.classpath.ForkedClassPath;
import org.springframework.boot.testsupport.web.servlet.DirtiesUrlFactories;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory; import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory; import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
@ -59,6 +65,7 @@ import static org.mockito.Mockito.mock;
* @author Ivan Sopov * @author Ivan Sopov
* @author Toshiaki Maki * @author Toshiaki Maki
*/ */
@DirtiesUrlFactories
class MultipartAutoConfigurationTests { class MultipartAutoConfigurationTests {
private AnnotationConfigServletWebServerApplicationContext context; private AnnotationConfigServletWebServerApplicationContext context;
@ -81,71 +88,41 @@ class MultipartAutoConfigurationTests {
assertThat(this.context.getBeansOfType(MultipartResolver.class)).hasSize(1); assertThat(this.context.getBeansOfType(MultipartResolver.class)).hasSize(1);
} }
@Test @ParameterizedTest(name = "{0}")
void webServerWithNoMultipartJettyConfiguration() { @MethodSource("webServerWithNoMultipartConfigurationArguments")
this.context = new AnnotationConfigServletWebServerApplicationContext(WebServerWithNoMultipartJetty.class, @ForkedClassPath
BaseConfiguration.class); void webServerWithNoMultipartConfiguration(String server, Class<?> configuration) {
DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class); this.context = new AnnotationConfigServletWebServerApplicationContext(configuration, BaseConfiguration.class);
assertThat(servlet.getMultipartResolver()).isNotNull();
assertThat(this.context.getBeansOfType(StandardServletMultipartResolver.class)).hasSize(1); assertThat(this.context.getBeansOfType(StandardServletMultipartResolver.class)).hasSize(1);
assertThat(this.context.getBeansOfType(MultipartResolver.class)).hasSize(1); assertThat(this.context.getBeansOfType(MultipartResolver.class)).hasSize(1);
verifyServletWorks(); verifyServletWorks();
}
@Test
void webServerWithNoMultipartUndertowConfiguration() {
this.context = new AnnotationConfigServletWebServerApplicationContext(WebServerWithNoMultipartUndertow.class,
BaseConfiguration.class);
DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class);
verifyServletWorks();
assertThat(servlet.getMultipartResolver()).isNotNull();
assertThat(this.context.getBeansOfType(StandardServletMultipartResolver.class)).hasSize(1);
assertThat(this.context.getBeansOfType(MultipartResolver.class)).hasSize(1);
}
@Test
void webServerWithNoMultipartTomcatConfiguration() {
this.context = new AnnotationConfigServletWebServerApplicationContext(WebServerWithNoMultipartTomcat.class,
BaseConfiguration.class);
DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class);
assertThat(servlet.getMultipartResolver()).isNull();
assertThat(this.context.getBeansOfType(StandardServletMultipartResolver.class)).hasSize(1);
assertThat(this.context.getBeansOfType(MultipartResolver.class)).hasSize(1);
verifyServletWorks();
}
@Test
void webServerWithAutomatedMultipartJettyConfiguration() {
this.context = new AnnotationConfigServletWebServerApplicationContext(WebServerWithEverythingJetty.class,
BaseConfiguration.class);
this.context.getBean(MultipartConfigElement.class);
assertThat(this.context.getBean(StandardServletMultipartResolver.class)) assertThat(this.context.getBean(StandardServletMultipartResolver.class))
.isSameAs(this.context.getBean(DispatcherServlet.class).getMultipartResolver()); .isSameAs(this.context.getBean(DispatcherServlet.class).getMultipartResolver());
verifyServletWorks();
} }
@Test static Stream<Arguments> webServerWithNoMultipartConfigurationArguments() {
void webServerWithAutomatedMultipartTomcatConfiguration() { return Stream.of(Arguments.of("Jetty", WebServerWithNoMultipartJetty.class),
this.context = new AnnotationConfigServletWebServerApplicationContext(WebServerWithEverythingTomcat.class, Arguments.of("Tomcat", WebServerWithNoMultipartTomcat.class),
BaseConfiguration.class); Arguments.of("Undertow", WebServerWithNoMultipartUndertow.class));
new RestTemplate().getForObject("http://localhost:" + this.context.getWebServer().getPort() + "/",
String.class);
this.context.getBean(MultipartConfigElement.class);
assertThat(this.context.getBean(StandardServletMultipartResolver.class))
.isSameAs(this.context.getBean(DispatcherServlet.class).getMultipartResolver());
verifyServletWorks();
} }
@Test @ParameterizedTest(name = "{0}")
void webServerWithAutomatedMultipartUndertowConfiguration() { @MethodSource("webServerWithAutomatedMultipartConfigurationArguments")
this.context = new AnnotationConfigServletWebServerApplicationContext(WebServerWithEverythingUndertow.class, @ForkedClassPath
BaseConfiguration.class); void webServerWithAutomatedMultipartConfiguration(String server, Class<?> configuration) {
this.context = new AnnotationConfigServletWebServerApplicationContext(configuration, BaseConfiguration.class);
this.context.getBean(MultipartConfigElement.class); this.context.getBean(MultipartConfigElement.class);
verifyServletWorks(); verifyServletWorks();
assertThat(this.context.getBean(StandardServletMultipartResolver.class)) assertThat(this.context.getBean(StandardServletMultipartResolver.class))
.isSameAs(this.context.getBean(DispatcherServlet.class).getMultipartResolver()); .isSameAs(this.context.getBean(DispatcherServlet.class).getMultipartResolver());
} }
static Stream<Arguments> webServerWithAutomatedMultipartConfigurationArguments() {
return Stream.of(Arguments.of("Jetty", WebServerWithEverythingJetty.class),
Arguments.of("Tomcat", WebServerWithEverythingTomcat.class),
Arguments.of("Undertow", WebServerWithEverythingUndertow.class));
}
@Test @Test
void webServerWithNonAbsoluteMultipartLocationUndertowConfiguration() { void webServerWithNonAbsoluteMultipartLocationUndertowConfiguration() {
this.context = new AnnotationConfigServletWebServerApplicationContext( this.context = new AnnotationConfigServletWebServerApplicationContext(

View File

@ -16,10 +16,15 @@
package org.springframework.boot.autoconfigure.web.servlet; package org.springframework.boot.autoconfigure.web.servlet;
import java.util.stream.Stream;
import jakarta.servlet.ServletContextEvent; import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener; import jakarta.servlet.ServletContextListener;
import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.boot.testsupport.web.servlet.DirtiesUrlFactories;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory; import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory; import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
@ -39,39 +44,23 @@ import static org.mockito.Mockito.mock;
* *
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
@DirtiesUrlFactories
class ServletWebServerServletContextListenerTests { class ServletWebServerServletContextListenerTests {
@Test @ParameterizedTest(name = "{0}")
void registeredServletContextListenerBeanIsCalledByJetty() { @MethodSource("testConfiguration")
registeredServletContextListenerBeanIsCalled(JettyConfiguration.class); void registeredServletContextListenerBeanIsCalled(String serverName, Class<?> configuration) {
AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext(
ServletListenerRegistrationBeanConfiguration.class, configuration);
ServletContextListener servletContextListener = (ServletContextListener) context
.getBean("registration", ServletListenerRegistrationBean.class).getListener();
then(servletContextListener).should().contextInitialized(any(ServletContextEvent.class));
context.close();
} }
@Test @ParameterizedTest(name = "{0}")
void registeredServletContextListenerBeanIsCalledByTomcat() { @MethodSource("testConfiguration")
registeredServletContextListenerBeanIsCalled(TomcatConfiguration.class); void servletContextListenerBeanIsCalled(String serverName, Class<?> configuration) {
}
@Test
void registeredServletContextListenerBeanIsCalledByUndertow() {
registeredServletContextListenerBeanIsCalled(UndertowConfiguration.class);
}
@Test
void servletContextListenerBeanIsCalledByJetty() {
servletContextListenerBeanIsCalled(JettyConfiguration.class);
}
@Test
void servletContextListenerBeanIsCalledByTomcat() {
servletContextListenerBeanIsCalled(TomcatConfiguration.class);
}
@Test
void servletContextListenerBeanIsCalledByUndertow() {
servletContextListenerBeanIsCalled(UndertowConfiguration.class);
}
private void servletContextListenerBeanIsCalled(Class<?> configuration) {
AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext( AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext(
ServletContextListenerBeanConfiguration.class, configuration); ServletContextListenerBeanConfiguration.class, configuration);
ServletContextListener servletContextListener = context.getBean("servletContextListener", ServletContextListener servletContextListener = context.getBean("servletContextListener",
@ -80,13 +69,10 @@ class ServletWebServerServletContextListenerTests {
context.close(); context.close();
} }
private void registeredServletContextListenerBeanIsCalled(Class<?> configuration) { static Stream<Arguments> testConfiguration() {
AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext( return Stream.of(Arguments.of("Jetty", JettyConfiguration.class),
ServletListenerRegistrationBeanConfiguration.class, configuration); Arguments.of("Tomcat", TomcatConfiguration.class),
ServletContextListener servletContextListener = (ServletContextListener) context Arguments.of("Undertow", UndertowConfiguration.class));
.getBean("registration", ServletListenerRegistrationBean.class).getListener();
then(servletContextListener).should().contextInitialized(any(ServletContextEvent.class));
context.close();
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)

View File

@ -16,11 +16,14 @@
package org.springframework.boot.autoconfigure.websocket.servlet; package org.springframework.boot.autoconfigure.websocket.servlet;
import jakarta.websocket.server.ServerContainer; import java.util.stream.Stream;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import jakarta.websocket.server.ServerContainer;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.boot.testsupport.web.servlet.DirtiesUrlFactories;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory; import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor; import org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor;
@ -36,41 +39,27 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
@DirtiesUrlFactories
class WebSocketServletAutoConfigurationTests { class WebSocketServletAutoConfigurationTests {
private AnnotationConfigServletWebServerApplicationContext context; @ParameterizedTest(name = "{0}")
@MethodSource("testConfiguration")
@BeforeEach void serverContainerIsAvailableFromTheServletContext(String server, Class<?>... configuration) {
void createContext() { try (AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext(
this.context = new AnnotationConfigServletWebServerApplicationContext(); configuration)) {
} Object serverContainer = context.getServletContext()
.getAttribute("jakarta.websocket.server.ServerContainer");
@AfterEach assertThat(serverContainer).isInstanceOf(ServerContainer.class);
void close() {
if (this.context != null) {
this.context.close();
} }
} }
@Test static Stream<Arguments> testConfiguration() {
void tomcatServerContainerIsAvailableFromTheServletContext() { return Stream.of(
serverContainerIsAvailableFromTheServletContext(TomcatConfiguration.class, Arguments.of("Jetty",
WebSocketServletAutoConfiguration.TomcatWebSocketConfiguration.class); new Class<?>[] { JettyConfiguration.class,
} WebSocketServletAutoConfiguration.JettyWebSocketConfiguration.class }),
Arguments.of("Tomcat", new Class<?>[] { TomcatConfiguration.class,
@Test WebSocketServletAutoConfiguration.TomcatWebSocketConfiguration.class }));
void jettyServerContainerIsAvailableFromTheServletContext() {
serverContainerIsAvailableFromTheServletContext(JettyConfiguration.class,
WebSocketServletAutoConfiguration.JettyWebSocketConfiguration.class);
}
private void serverContainerIsAvailableFromTheServletContext(Class<?>... configuration) {
this.context.register(configuration);
this.context.refresh();
Object serverContainer = this.context.getServletContext()
.getAttribute("jakarta.websocket.server.ServerContainer");
assertThat(serverContainer).isInstanceOf(ServerContainer.class);
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)