I use default Tomcat embedded container. However, in some of my tests I use Wiremock (using Jetty underneath). This makes my integration tests run against Jetty server, not Tomcat.
Is there any way to force Spring Boot to stick with Tomcat ?
I use default Tomcat embedded container. However, in some of my tests I use Wiremock (using Jetty underneath). This makes my integration tests run against Jetty server, not Tomcat.
Is there any way to force Spring Boot to stick with Tomcat ?
As Stéphane Nicoll stated here you should define an empty TomcatEmbeddedServletContainerFactory
@Bean
Simply adding such bean was not sufficient for me. I got 'multiple beans' exception. As I was adding that to a custom test starter I just had to make sure, it is added before EmbeddedServletContainerAutoConfiguration
resolution took place, i.e:
@Configuration
@AutoConfigureBefore(EmbeddedServletContainerAutoConfiguration.class)
public class ForceTomcatAutoConfiguration {
@Bean
TomcatEmbeddedServletContainerFactory tomcat() {
return new TomcatEmbeddedServletContainerFactory();
}
}
Edit: In Spring Boot 2.0 this works for me:
@Configuration
@AutoConfigureBefore(ServletWebServerFactoryAutoConfiguration.class)
public class ForceTomcatAutoConfiguration {
@Bean
TomcatServletWebServerFactory tomcat() {
return new TomcatServletWebServerFactory();
}
}
With Spring Boot 2.3 the above solution hasn't worked for me.
Perhaps I have special circumstances with not having a SpringBootApplication
in the main classes. I have only a SpringBootApplication
in the test classes.
Anyhow this has worked for me:
@SpringBootApplication(exclude = org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration.class)
public class TestApplication {
}
with Spring Boot 2.5.10
@AutoConfigureBefore(ServletWebServerFactoryAutoConfiguration.class)
didn't work for me.
but as sven doring mentioned excluding EmbeddedWebServerFactoryCustomizerAutoConfiguration
worked for me.
it even worked for external tomcat
code is below
@SpringBootApplication(exclude = org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration.class)