Spring

JUnit 4 Test 에서 get 메소드 에러 관련

쿠카이든 2022. 3. 1. 09:31
728x90
  • 스프링 부트와 AWS로 혼자 구현하는 웹서비스(p. 62, 이동욱님 저)를 따라 소스를 구현하다보니, 아래 JUnit 4 테스트의 get 부분에서 에러가 났다.
package com.jojoldu.book.awsspring.springboot.web;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.;

@ExtendWith(SpringExtension.class) // 스프링부트 테스트와 JUnit 사이에 연결자 역할
@WebMvcTest(controllers = HelloController.class) // Web(Spring MVC)에 집중할 수 있는 어노테이션
public class HelloControllerTest {

@Autowired // 스프링이 관리하는 Bean 주입 받기
private MockMvc mvc; // 웹 API 테스트(GET, POST 등 API 테스트)

@Test
public void returnhelloOk() throws Exception{
    String hello = "hello";

    mvc.perform(get("/hello")) // MockMvc 통해 /hello 주소로 HTTP GET 요청 (아래 검증기능 이어서 선언 가능)
            .andExpect(status().isOk()) // mvc.perform 결과 검증, HTTP Header Status 검증
            .andExpect(content().string(hello)); // Controller => "hello" 리턴 값 맞는지 검증
	}
}
  • 검색해보니 해결법은 의외로 간단했다. 코드 import 부분에서 MockMvcResultMatchers 밑에 아래 한줄을 추가하니 오류가 해소되었다.
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.;

 

출처 : https://github.com/jojoldu/freelec-springboot2-webservice/issues/740

728x90