junit – 集成测试基于Spring Boot的微服务

前端之家收集整理的这篇文章主要介绍了junit – 集成测试基于Spring Boot的微服务前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经阅读了很多关于使用 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)配置对我有用:

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-maven-plugin</artifactId>
  6. <version>1.3.5.RELEASE</version>
  7. <executions>
  8. <execution>
  9. <id>pre-integration-test</id>
  10. <goals>
  11. <goal>start</goal>
  12. </goals>
  13. <configuration>
  14. <skip>${integration-tests.skip}</skip>
  15. </configuration>
  16. </execution>
  17. <execution>
  18. <id>post-integration-test</id>
  19. <goals>
  20. <goal>stop</goal>
  21. </goals>
  22. <configuration>
  23. <skip>${integration-tests.skip}</skip>
  24. </configuration>
  25. </execution>
  26. </executions>
  27. </plugin>
  28.  
  29. <plugin>
  30. <groupId>org.apache.maven.plugins</groupId>
  31. <artifactId>maven-failsafe-plugin</artifactId>
  32. <version>2.19.1</version>
  33. <configuration>
  34. <skip>${integration-tests.skip}</skip>
  35. <includes>
  36. <include>**/*IT.java</include>
  37. </includes>
  38. </configuration>
  39. <executions>
  40. <execution>
  41. <goals>
  42. <goal>integration-test</goal>
  43. <goal>verify</goal>
  44. </goals>
  45. </execution>
  46. </executions>
  47. </plugin>
  48.  
  49. <plugin>
  50. <groupId>com.bazaarvoice.maven.plugins</groupId>
  51. <artifactId>process-exec-maven-plugin</artifactId>
  52. <version>0.7</version>
  53. <executions>
  54. <execution>
  55. <id>switchboard-process</id>
  56. <phase>pre-integration-test</phase>
  57. <goals>
  58. <goal>start</goal>
  59. </goals>
  60. <configuration>
  61. <name>accounts-service</name>
  62. <workingDir>/../../micro-service</workingDir>
  63. <waitForInterrupt>false</waitForInterrupt>
  64. <arguments>
  65. <argument>java</argument>
  66. <argument>-jar</argument>
  67. <argument>${basedir}/../micro-service/target/micro-service-${project.version}-exec.jar</argument>
  68. </arguments>
  69. </configuration>
  70. </execution>
  71. <!--Stop all processes in reverse order-->
  72. <execution>
  73. <id>stop-all</id>
  74. <phase>post-integration-test</phase>
  75. <goals>
  76. <goal>stop-all</goal>
  77. </goals>
  78. </execution>
  79. </executions>
  80. </plugin>
  81. </plugins>
  82. </build>

在test.java文件夹中有一个Integration Test类(WebServerIT):

  1. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  2. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
  3.  
  4. @RunWith(SpringJUnit4ClassRunner.class)
  5. @SpringApplicationConfiguration(classes = WebServerApp.class)
  6. @WebIntegrationTest("server.port:0")
  7. public class WebServerIT {
  8.  
  9. @Autowired
  10. private WebApplicationContext webServerAppContext;
  11.  
  12. private MockMvc webServerMockMvc;
  13.  
  14. @Before
  15. public void setUp() {
  16. System.out.println("the test is set up");
  17. webServerMockMvc = MockMvcBuilders.webAppContextSetup(webServerAppContext).build();
  18. }
  19.  
  20. /**
  21. * This test calls the WebServer's endpoint (/accounts/123456789) which in turn calls the micro-service rest api
  22. * which is started using the process-exec-maven-plugin,otherwise the test would fail.
  23. */
  24. @Test
  25. public void testWebServerInteractionWithMicroService() throws Exception {
  26. this.webServerMockMvc.perform(get("/accounts/123456789"))
  27. .andExpect(status().isOk());
  28. }
  29. }

猜你在找的Java相关文章