我已经阅读了很多关于使用
Spring Boot和RESTful服务的指南,其中许多包含有关运行单元测试的信息,最着名的是“使用Spring Boot构建应用程序”.但是,我还没有看到任何有关如何对使用/依赖其他Spring Boot应用程序的Spring Boot应用程序进行单元测试的示例,这在云微服务架构中很常见.因此,例如,我们有以下Spring Boot服务:
ServiceMediator,
适配器1,
适配器2
ServiceMediator根据输入调用Adapter1或Adapter2.
有没有办法在Spring JUnit测试中启动和测试ServiceMediator之前启动Spring Boot服务Adapter1和Adapter2?
解决方法
process-exec-maven-plugin可能会有所帮助,因为它允许在预集成测试阶段启动多个java进程(作为标准的Spring Boot应用程序),并且它会在集成后测试阶段自动关闭它们.
注意:集成测试应该在集成测试阶段运行,因为maven-failsafe-plugin应该配置spring-boot-maven-plugin see.
然后运行我们的集成测试验证或更高的maven生命周期应该成为目标,因为集成测试阶段实际上位于包之间并验证Lifecycles see Default Lifecycles.
以下maven(pom.xml)配置对我有用:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.5.RELEASE</version>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
<configuration>
<skip>${integration-tests.skip}</skip>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
<configuration>
<skip>${integration-tests.skip}</skip>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skip>${integration-tests.skip}</skip>
<includes>
<include>**/*IT.java</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.bazaarvoice.maven.plugins</groupId>
<artifactId>process-exec-maven-plugin</artifactId>
<version>0.7</version>
<executions>
<execution>
<id>switchboard-process</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<name>accounts-service</name>
<workingDir>/../../micro-service</workingDir>
<waitForInterrupt>false</waitForInterrupt>
<arguments>
<argument>java</argument>
<argument>-jar</argument>
<argument>${basedir}/../micro-service/target/micro-service-${project.version}-exec.jar</argument>
</arguments>
</configuration>
</execution>
<!--Stop all processes in reverse order-->
<execution>
<id>stop-all</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop-all</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
在test.java文件夹中有一个Integration Test类(WebServerIT):
import static org.springframework.test.web.servlet.result.mockmvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.mockmvcResultMatchers.content;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = WebServerApp.class)
@WebIntegrationTest("server.port:0")
public class WebServerIT {
@Autowired
private WebApplicationContext webServerAppContext;
private mockmvc webServermockmvc;
@Before
public void setUp() {
System.out.println("the test is set up");
webServermockmvc = mockmvcBuilders.webAppContextSetup(webServerAppContext).build();
}
/**
* This test calls the WebServer's endpoint (/accounts/123456789) which in turn calls the micro-service rest api
* which is started using the process-exec-maven-plugin,otherwise the test would fail.
*/
@Test
public void testWebServerInteractionWithMicroService() throws Exception {
this.webServermockmvc.perform(get("/accounts/123456789"))
.andExpect(status().isOk());
}
}