[SpringBoot]HelloTestController

김민수·2022년 4월 29일

https://github.com/MSlcim/SelfStudy
test패키지

@RunWith

  • 테스트를 진행할 때 JUnit에 내장된 실행자 외에 다른 실행자를 실행시킨다.
  • 스프링 부트 테스트와 JUnit 사이에 연결자 역할.

@WebMvcTest

  • 선언할 경우 @Controller, @ControllerAdvice 를 사용가능.
  • @Service, @Component, @Repository 사용불가능.

@Autowired

  • Bean을 주입 받음.

private MockMvc mvc

  • 웹 API를 테스트할 때 사용.
  • 스프링 MVC테스트의 시작점
  • 이 클래스를 통해 HTTP GET, POST등에 대한 API테스트를 할 수 있음.

mvc.perform(get("/hello))

  • MockMvc를 통해 /hello 주소로 get요청.

.andExpect(status().isOk())

  • mvc.perform의 결과를 검증.
  • HTTP Header의 Status검증합니다.
  • 200,404,500 등의 상태를 검증합니다.
  • 여기선 200인지 아닌지 검증.

.andExpect(content().string(hello))

  • mvc.perform의 결과를 검증합니다.
  • 응답 본문의 내용을 검증.
  • Controller에서 "hello"를 리턴하기 때문에 이 값이 맞는지 검증.

test를 run할때 오류가 발생했는데

  • 인텔리제이 설정 에서 build Tools > Run tests using 항목을 IntelliJ IDEA로 변경한다 -->해결
profile
Hello Minsu World

0개의 댓글