[스프링 부트] Hello Controller 테스트 코드 작성하기

HL·2021년 3월 9일
0

스프링 부트

목록 보기
1/4

컨트롤러 생성

코드

package com.hl.book.springboot.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }
}

설명

  • @RestController
    • JSON 형식으로 리턴하게 해줌
    • 예전에는 각 메소드마다 @ResponseBody 를 선언해야 했음
  • @GetMapping
    • Get 요청을 받을 수 있게 해줌
    • 예전에는 @RequestMapping(method=RequestMethod.GET) 을 사용해야 했음

테스트 코드 작성

코드

package com.hl.book.springboot.web;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

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

@RunWith(SpringRunner.class)	// (1)
@WebMvcTest(controllers = HelloController.class)	// (2)
public class HelloControllerTest {

    @Autowired	// (3)
    private MockMvc mvc;	// (4)

    @Test
    public void hello_리턴() throws Exception{
        String hello = "hello";
        mvc.perform(get("/hello"))	// (5)
                .andExpect(status().isOk())	// (6)(7)
                .andExpect(content().string(hello));	// (8)
    }
}

설명

  1. @RunWith(SpringRunner.class)

    • JUnit에 내장된 실행자 외 다른 실행자를 실행
      • 여기서는 SpringRunner 실행자
    • 즉, 스프링 부트 테스트와 JUnit 사이에 연결자 역할
  2. @WebMvcTest

    • Web(Spring MVC)에 집중하게 해줌 (?)
    • @Controller, @ControllerAdvice 사용 가능
    • @Service, @Component, @Repository 사용 불가
  3. @Autowired

    • 스프링이 관리하는 Bean 을 주입 받음
      • Bean : Spring IoC 컨테이너가 관리하는 자바 객체
  4. private MockMvc mvc

    • 스프링 MVC 테스트의 시작점
    • GET, POST 등 테스트하게 해줌
    • 사용자 역할?
  5. mvc.perform(get("/hello"))

    • MockMvc 를 통해 GET 요청
    • 체이닝 지원
  6. .andExpect()

    • mvc.perform 의 결과 검증
  7. status().isOk()

    • HTTP Header 의 Status 가 200 인지
  8. content().string("hello")

    • 본문의 내용이 "hello" 인지
profile
Swift, iOS 앱 개발을 공부하고 있습니다

0개의 댓글