[테스트코드] 인수테스트 - AOP

Welcome to Seoyun Dev Log·2022년 8월 28일

테스트코드

목록 보기
5/7

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

controller 테스트 코드 생성

  • 1.환경을 지정 : @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    RANDOM_PORT -> port 번호를 8080이 아닌 랜덤포트로 실제 띄워주도록 설정
package lotto.practice.random.controller;

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

import java.util.HashMap;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

@ActiveProfiles(profiles = "test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AcceptanceTest {

    @LocalServerPort
    int port;

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

    @Test
    public void request() {
        //given
        Map param = new HashMap<>();

        param.put("userNo", 1);
        param.put("lottotype", "ALLAUTO");

        //when
        ExtractableResponse<Response> response = getRequest("/machine/lottoNum", param);

        //then
        assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
    }

    public static ExtractableResponse<Response> getRequest(String path, Map<String, Object> param) {
        return RestAssured
                .given().log().all().queryParams(param).contentType(MediaType.APPLICATION_JSON_VALUE)
                .when().get(path)
                .then().log().all()
                .extract();
    }

    public static <T> ExtractableResponse<Response> postRequest(String path, T param) {
        return RestAssured
                .given().log().all().body(param).contentType(MediaType.APPLICATION_JSON_VALUE)
                .when().post(path)
                .then().log().all()
                .extract();
    }

}
  • 2.bulid.gradle 디펜던시 로드
	testImplementation 'io.rest-assured:rest-assured:3.3.0'
    1. RestAssured port 지정 -> 기본 틀
@LocalServerPort
    int port;
    
 @BeforeEach
    public void environmentSetUp() {
        RestAssured.port = port;
    }
    1. 엔트포인트 요청에 따른 코드
      get요청
public static ExtractableResponse<Response> getRequest(String path, Map<String, Object> param) {
        return RestAssured
                .given().log().all().queryParams(param).contentType(MediaType.APPLICATION_JSON_VALUE)
                .when().get(path)
                .then().log().all()
                .extract();
    }

post요청

public static <T> ExtractableResponse<Response> postRequest(String path, T param) {
        return RestAssured
                .given().log().all().body(param).contentType(MediaType.APPLICATION_JSON_VALUE)
                .when().post(path)
                .then().log().all()
                .extract();
    }
  • 파라미터의 경우 map을 사용해서 파라미터를 넘겨준다
profile
하루 일지 보단 행동 고찰 과정에 대한 개발 블로그

0개의 댓글