Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiate multiple spring boot apps in test

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?

like image 597
aleksei Avatar asked Jan 03 '23 01:01

aleksei


1 Answers

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.

like image 157
StanislavL Avatar answered Jan 05 '23 16:01

StanislavL