Instantiate multiple spring boot apps in test

我的梦境 提交于 2020-01-11 09:45:11

问题


I have several instances of my spring boot app, which in parallel do some work with DB. Each instance is running in separate JVM.
Is it a way to write a test in Java for testing that on one JVM? Like following:

  1. Setup some embedded DB for testing purposes or even just mock it.
  2. Start 2-5 instances of my Spring boot app
  3. Wait some time
  4. Stop all started instances
  5. Verify DB and check that all the conditions are met.

Each instance has its own context and classpath.
I think that I could achieve that with some shell script scenario but I'd like to make it in Java.
What would be the best approach here?


回答1:


You can run them multiple times using different ports.

I did something similar

@RunWith(SpringJUnit4ClassRunner.class)
public class ServicesIntegrationTest {

    private RestTemplate restTemplate = new RestTemplate();

    @Test
    public void runTest() throws Exception {
        SpringApplicationBuilder uws = new SpringApplicationBuilder(UserWebApplication.class)
                .properties("server.port=8081",
                        "server.contextPath=/UserService",
                        "SOA.ControllerFactory.enforceProxyCreation=true");
        uws.run();

        SpringApplicationBuilder pws = new SpringApplicationBuilder(ProjectWebApplication.class)
                .properties("server.port=8082",
                        "server.contextPath=/ProjectService",
                        "SOA.ControllerFactory.enforceProxyCreation=true");
        pws.run();

        String url = "http://localhost:8081/UserService/users";
        ResponseEntity<SimplePage<UserDTO>> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<SimplePage<UserDTO>>() {
                });

here the source.



来源:https://stackoverflow.com/questions/47866954/instantiate-multiple-spring-boot-apps-in-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!