JUnit – integration testing of spring boot based microservices
I have read a lot of guides about using spring boot and restful services, many of which contain information about running unit tests. The most famous is "building applications using spring boot" However, I haven't seen any examples of how to unit test spring boot applications that use / rely on other spring boot applications, which is common in cloud microservice architecture Therefore, for example, we have the following spring boot services:
Servicemediator, adapter 1, adapter 2
Servicemediator calls adapter1 or Adapter2 according to the input
Is there any way to start spring boot service adapter1 and Adapter2 before starting and testing servicemediator in spring JUnit test?
Solution
The process exec Maven plugin may be helpful because it allows multiple Java processes (as standard spring boot applications) to be started during the pre integration test phase, and it automatically closes them during the post integration test phase
Note: the integration test should be run during the integration test phase because the Maven failsafe plugin should be configured with spring boot Maven plugin see Then running our integration test verification or higher Maven lifecycle should be the goal, because the integration test phase actually lies between packages and verifies lifecycles see default lifecycles
The following Maven (POM. XML) configuration is useful to me:
<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>
In test There is an integration test class (webserverit) in the Java folder:
import static org.springframework.test.web.servlet.result.@R_301_1424@ResultMatchers.status; import static org.springframework.test.web.servlet.result.@R_301_1424@ResultMatchers.content; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = WebServerApp.class) @WebIntegrationTest("server.port:0") public class WebServerIT { @Autowired private WebApplicationContext webServerAppContext; private @R_301_1424@ webServer@R_301_1424@; @Before public void setUp() { System.out.println("the test is set up"); webServer@R_301_1424@ = @R_301_1424@Builders.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.webServer@R_301_1424@.perform(get("/accounts/123456789")) .andExpect(status().isOk()); } }