Spring
[스프링 부트와 AWS로 혼자 구현하는 웹 서비스] p.62 실행 오류 해결법
쿠카이든
2022. 5. 16. 10:19
728x90
오류 로그
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;
@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" 리턴 값 맞는지 검증
}
mvc.perform부터 오류가 난다.
import org.springframework.test.web.servlet.MockMvc; 밑에 줄에
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.;
를 추가하면 해결된다.
이유 - requestBuilder- 실행할 요청을 준비하는 데 사용되는데 위에 두줄을 임포트함으로써 준비가 완료된다고 한다.
출처 : https://github.com/jojoldu/freelec-springboot2-webservice/issues/740, https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/web/servlet/MockMvc.html
728x90