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

时间:2016-07-01 09:29:29

标签: java spring gradle spring-boot integration-testing

我的春季启动应用程序有一些集成测试。基于依赖关系(和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 :(得分: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集编写测试,您应该准备好了。