이슈
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);
}
}
'미니멀 개발일기' 카테고리의 다른 글
주석중독 (0) | 2025.01.08 |
---|---|
Intelij IDEA 백그라운드 화면 바꿈 (0) | 2025.01.08 |
ParameterizedTest 적용해보기 (0) | 2024.12.16 |
Test Data Builder Class를 적용하기 전의 고찰 (0) | 2024.12.10 |
Junit5 디펜던시 추가에 4시간을 쓴 경험 (0) | 2024.12.04 |