복붙노트

[SPRING] 테스트에서 여러 개의 스프링 부팅 응용 프로그램 인스턴스화

SPRING

테스트에서 여러 개의 스프링 부팅 응용 프로그램 인스턴스화

몇 가지 인스턴스를 내 봄 부팅 애플 리케이션, 병렬로 일부 DB와 함께 작동하지. 각 인스턴스는 별도의 JVM에서 실행됩니다. 하나의 JVM에서이를 테스트하기 위해 Java로 테스트를 작성하는 방법입니까? 다음과 같이 :

각 인스턴스에는 자체 컨텍스트와 클래스 경로가 있습니다. 필자는 쉘 스크립트 시나리오를 통해이 문제를 해결할 수 있다고 생각하지만 자바로 만들고 싶습니다. 여기서 가장 좋은 방법은 무엇입니까?

해결법

  1. ==============================

    1.다른 포트를 사용하여 여러 번 실행할 수 있습니다.

    다른 포트를 사용하여 여러 번 실행할 수 있습니다.

    나는 비슷한 것을했다.

    @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>>() {
                    });
    

    여기 소스.

  2. from https://stackoverflow.com/questions/47866954/instantiate-multiple-spring-boot-apps-in-test by cc-by-sa and MIT license