objectMapper); ObjectMapperConfig jackson2ObjectMapperConfig = new ObjectMapperConfig(jackson2Mapper); RestAssured.config = RestAssuredConfig.config() .objectMapperConfig(jackson2ObjectMapperCon"> objectMapper); ObjectMapperConfig jackson2ObjectMapperConfig = new ObjectMapperConfig(jackson2Mapper); RestAssured.config = RestAssuredConfig.config() .objectMapperConfig(jackson2ObjectMapperCon"> objectMapper); ObjectMapperConfig jackson2ObjectMapperConfig = new ObjectMapperConfig(jackson2Mapper); RestAssured.config = RestAssuredConfig.config() .objectMapperConfig(jackson2ObjectMapperCon">
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AcceptanceTestBase {

    protected static final String BASE_URL = "<http://localhost>";

    @LocalServerPort
    protected int port;

    @Autowired
    protected ObjectMapper objectMapper;

    protected String[] ignoringFieldsForResponseWithId = new String[]{"createdDateTime", "modifiedDateTime", "id"};

    protected String[] ignoringFieldsForResponse = new String[]{"createdDateTime", "modifiedDateTime"};

    protected String[] ignoringFieldsForErrorResponse = new String[]{"timestamp"};

    {
        setUpRestAssured();
    }

    private void setUpRestAssured() {
        Jackson2Mapper jackson2Mapper = new Jackson2Mapper((type, charset) -> objectMapper);
        ObjectMapperConfig jackson2ObjectMapperConfig = new ObjectMapperConfig(jackson2Mapper);

        RestAssured.config = RestAssuredConfig.config()
                                              .objectMapperConfig(jackson2ObjectMapperConfig);
    }
}

기본 설정은 AcceptanceTestBase 클래스를 상속하는 것으로 한다.

@ParameterizedTest
@MethodSource("readAllProvider")
@DisplayName("")
void readAll(String desc, GroupRequest givenGroupRequest,GroupResponses expectedGroupResponses) {
    String path = "/api/groups";
    RequestSpecification givenRequest = RestAssured.given()
        .baseUri(BASE_URL)
        .port(port)
        .basePath(path);

    Response actualResponse = givenRequest.when()
        .log().all()
        .get()
        .andReturn();

    then(actualResponse)
        .as("사용자가 가입한 전체 그룹 조회 : %s", desc)
        .hasFieldOrPropertyWithValue("statusCode", HttpStatus.OK.value())
        .extracting(response -> response.as(GroupResponses.class))
        .usingRecursiveComparison()
        .isEqualTo(expectedGroupResponses);
}
  1. @DisplayName에는 유스케이스의 개요를 적어준다.
  2. MethodSource클래스 네이밍은 *Provider로 한다.
  3. given에 사용되는 매개변수는 given을 접두어로 사용한다.
  4. expected로 사용되는 매개변수는 expected를 접두어로 사용한다.
  5. when의 결과로 나오는 Response는 actualResponse라고 명명한다.
  6. then절은 BDDAssertions.then 사용한다.
  7. as에 테스트케이스에 대한 설명을 작성한다.

참고

https://happy-coding-day.tistory.com/177

https://www.baeldung.com/rest-assured-tutorial

https://github.com/Sc0up/scoup-backend/issues/5