Spring [SOLVED]: Instantiate multiple spring boot apps in test

Spring [SOLVED]: Instantiate multiple spring boot apps in test

Home Forums Frameworks Spring Spring [SOLVED]: Instantiate multiple spring boot apps in test

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #243801

    Cloudy Point
    Keymaster

    QuestionQuestion

    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?

    #243802

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    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.

    Source: https://stackoverflow.com/questions/47866954/instantiate-multiple-spring-boot-apps-in-test
    Author: StanislavL
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.