【发布时间】:2018-12-09 13:07:54
【问题描述】:
我有一个包含两个项目的多模块项目:backend 和 client。后端是一个普通的 Spring Boot Rest API,没什么特别的。 client 模块只是一个使用 Rest API 的 Java 库。
后端也使用JSP封装了“war”作为后端,需要部署到servlet容器中。使用@SpringBootTest 仍然可以轻松测试后端。
现在我想在 client 模块中使用后端模块作为沙箱服务器进行一些集成测试。
要使用我添加的客户端模块中的所有后端类
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
并将后端配置为带有类的客户端中的测试依赖项
在我的 client/src/test/java 中,我有一个帮助类来启动后端模块
@Configuration
public class SandboxServer {
@Bean
public ConfigurableApplicationContext backend() {
return
new SpringApplicationBuilder(BackendApplication.class)
.sources(SandboxServerConfig.class)
.run("spring.profiles.active=sandbox")
}
}
配置文件“沙盒”用于设置测试数据库等。但我遇到了更多问题。第一个问题是关于文档根目录,所以我配置了它:
public class SandboxServerConfig
implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
@Override
public void customize(TomcatServletWebServerFactory factory) {
factory.setDocumentRoot(new File("../backend/src/main/webapp"));
}
}
但它仍然不起作用,因为 Spring 没有拾取 backend/src/main/resources/application.properties
这可能是正确的,因为它不在客户端模块的根类路径中。
所以它并没有真正起作用。我想在集成测试中只启动兄弟模块是不可能的。
如何实现启动兄弟spring boot模块进行集成测试?像这样的场景的最佳做法是什么?
【问题讨论】:
-
您决定部署到单独的 servlet 容器而不是使用嵌入式服务器的具体原因是什么——这是 Spring Boot 应用程序的推荐部署机制。似乎这个决定让你陷入了困境,让你有一些非常奇怪的解决方法。
-
它是一个嵌入式服务器。但是它使用了一些 JSP 页面,所以我还有一个 src/main/webapp 文件夹。
-
如果它是一个嵌入式服务器,那么整个
.war的东西完全是假的。 .war` 部署在外部 servlet 上。
标签: java spring spring-boot testing integration-testing