미니멀 개발일기

local port와 rest-assured port 동기화

SH3542 2025. 1. 5. 21:09

 

 

이슈

rest-assured, junit5를 이용한 rest api test 도중,

server.port를 8080 외의 값 으로 변경하면 테스트 실패

 

 

원인

@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)

 

- 해당 설정(DEFINED_PORT)은, 서버를 mocking 하지 않고 지정된 port로 실제 구동

- port 번호는 server.port 환경 변수 값 사용 (defalut 8080)

 

 

- server.port를 수정하면, server가 켜지는 port는 바뀌지만, api 테스트간 rest-assured 라이브러리가 사용하는 port는 바뀌지 않음

- rest-assured는 여전히 localhost:8080/.. 으로 요청을 보내게 되므로, 없는 서버 참조로 인한 Connection Error 발생

 

 

 

application.properties

spring.config.import=classpath:application-secret.properties

server.port=8080
spring.application.name=ytb
logging.level.org.springframework=INFO

 

 

원본 코드

package com.sh.ytb;

import io.restassured.RestAssured;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.context.TestPropertySource;

@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
@TestPropertySource(locations = "file:src/main/resources/application.properties")
class ApplicationTests {
  
  @Test
  void shouldReturnCorrectHttpStatusAndResponse() {
    String expectedMsg = "hello client";

    final ExtractableResponse<Response> response = RestAssured.given().log().all()
        .contentType(MediaType.APPLICATION_JSON_VALUE)
        .when().get("/youtube").then().log().all().extract();

    Assertions.assertEquals(response.statusCode(), HttpStatus.OK.value());
    Assertions.assertEquals(response.body().asString(), expectedMsg);
  }
}

 

 

해결 1. 요청마다 port 동기화

.contentType(MediaType.APPLICATION_JSON_VALUE).port(22)

 

 

해결 2. local port를 참조하여 rest-assured 공용 port와 동기화

package com.sh.ytb;

import io.restassured.RestAssured;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.context.TestPropertySource;

@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
@TestPropertySource(locations = "file:src/main/resources/application.properties")
class ApplicationTests {

  @LocalServerPort
  private int port;

  @BeforeEach
  void setUp() {
    RestAssured.port = port;
  }

  @Test
  void shouldReturnCorrectHttpStatusAndResponse() {
    String expectedMsg = "hello client";

    final ExtractableResponse<Response> response = RestAssured.given().log().all()
        .contentType(MediaType.APPLICATION_JSON_VALUE).port(22)
        .when().get("/youtube").then().log().all().extract();

    Assertions.assertEquals(response.statusCode(), HttpStatus.OK.value());
    Assertions.assertEquals(response.body().asString(), expectedMsg);
  }
}