[SpringBoot + JPA 프로젝트] (1) MainController 통합테스트 진행

반응형
728x90
반응형

공통 Response

ResponseDto.java
package com.api.westmall.common;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Builder
public class ResponseDto<T> {
    private int status;
    private String message;
    private T body;
}

 

CommonResponse.java
package com.api.westmall.common;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class CommonResponse {
    public <T> ResponseEntity<ResponseDto<T>> send(T body, HttpStatus status, String message) {
        ResponseDto<T> responseDto = ResponseDto.<T>builder()
                .status(status.value())
                .message(message)
                .body(body)
                .build();

        return ResponseEntity.status(status).body(responseDto);
    }

    public ResponseEntity<?> send() {
        return this.send(null, HttpStatus.OK, "");
    }

    public <T> ResponseEntity<ResponseDto<T>> send(T object) {
        return this.send(object, HttpStatus.OK, "");
    }
}

 

 

 

 

테스트 컨트롤러

MainController.java
package com.api.westmall.controller;

import com.api.westmall.common.CommonResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/main")
public class MainController {
    private final CommonResponse commonResponse;

    @GetMapping("/hello")
    public ResponseEntity<?> hello() {
        return commonResponse.send("hello westmall");
    }
}

 

 

 

통합테스트

pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

 

MainControllerTest.java
package com.api.westmall.controller;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
class MainControllerTest {
    @Autowired
    MockMvc mockMvc;

    @Test
    @DisplayName("테스트 페이지 response 체크")
    public void main() throws Exception {
        mockMvc.perform(get("/main/hello"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("body").exists())
                .andExpect(MockMvcResultMatchers.jsonPath("body").value("hello westmall"));
                ;
    }
}

 

결과
MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = [Content-Type:"application/json"]
     Content type = application/json
             Body = {"status":200,"message":"","body":"hello westmall"}
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

 

 

 

 

반응형

Designed by JB FACTORY