首页 文章

如何使用带有gradle的多个嵌入式服务器运行spring-boot集成测试

提问于
浏览
1

我有一些 Spring 季启动应用程序的集成测试 . 基于依赖关系(和classpath jars),spring boot选择一个服务器来启动:(tomcat只有spring-boot-starter-web,如果有spring-boot-starter-undertow或者如果有spring-boot-则是jetty起动码头)

我正在编写一个应该在许多不同服务器上运行的过滤器 . 我没有任何服务器上的编译依赖,但我想在许多服务器上测试我的代码 . 我该怎么做?

当然,一种方法是根据某些env变量设置gradle脚本集依赖关系,然后使用不同的env变量值调用 gradle test 几次 . 有没有更简单的方法,所以我可以一次测试一切?比如在测试中以编程方式启动服务器?或使用一些gradle / spring插件?

1 回答

  • 0

    我的建议是为所有三个服务器添加测试范围的依赖项,但在测试代码中创建三个单独的Spring Boot应用程序类,每个应用程序类禁用 EmbeddedServletContainerAutoConfiguration 并导入相应的服务器配置:

    @Profile("tomcat")
    @SpringBootApplication(exclude = EmbeddedServletContainerAutoConfiguration.class)
    @Import(EmbeddedServletContainerAutoConfiguration.EmbeddedTomcat.class)
    public class TomcatApplication {
        public static void main(String[] args) {
            TomcatApplication.run(TomcatApplication.class, args);
        }
    }
    
    @Profile("undertow")
    @SpringBootApplication(exclude = EmbeddedServletContainerAutoConfiguration.class)
    @Import(EmbeddedServletContainerAutoConfiguration.EmbeddedUndertow.class)
    public class UndertowApplication {
        public static void main(String[] args) {
            UndertowApplication.run(UndertowApplication.class, args);
        }
    }
    
    @Profile("jetty")
    @SpringBootApplication(exclude = EmbeddedServletContainerAutoConfiguration.class)
    @Import(EmbeddedServletContainerAutoConfiguration.EmbeddedJetty.class)
    public class JettyApplication {
        public static void main(String[] args) {
            JettyApplication.run(JettyApplication.class, args);
        }
    }
    

    然后,使用相应的 @ActiveProfiles 集编写测试,您应该准备好了 .

相关问题