콘텐츠로 이동

2022 11 08

2022-11-08

@AutoConfigureMockMvc

  • @WebMvcTest
    • 웹에서 테스트하기 힘든 컨트롤러 테스트
    • 시큐리티/필터 까지 자동으로 테스트
    • @MockBean + Controller Bean 조합
    • @Service, @Component, @Repository 사용 불가
  • @AutoConfigureMockMvc
    • @Service, @Repository 객체도 메모리에 모두 올림
    • MockMvc 보다 세밀하게 제어하기 위해 사용
    • 어플리케이션 전체에 대한 테스트에 쓰임 => @SpringBootTest 갬성

Connectable Controller Test

  • @WebMvcTest
    • controllers를 명시하여 어떤 컨트롤러를 로딩할지 명시
    • @Component로 등록되어있는 빈은 @WebMvcTest로 로딩 못함
      • 따라서 JwtAuthenticationFilter 등은 Exclude 해주자 -> 대신 @WithMockUser 도입
  • @MockBean(JpaMetamodelMappingContext.class)
    • 참고: https://1-7171771.tistory.com/136
    • 없으면 JPA metamodel must not be empty! 에러가 뜸
    • 기본적으로 @WebMvcTest는 JPA 생성 기능이 없음
      • 하지만 앱에서는 @EnableJpaAuditing 을 통해 빈 어딧팅
    • JpaMetamodelMappingContext를 모킹하면서 해결하자