Error creating bean with name 'jpaAuditingHandler':

pudding·2023년 1월 11일
0

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaAuditingHandler': Cannot resolve reference to bean 'jpaMappingContext' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: JPA metamodel must not be empty!

  • Error creating bean with name 'jpaAuditingHandler':
    처음 test 코드를 실행하자마자 마주한 오류이다. Auditing과 관련된 오류일 거라고 추측했다.
  • JPA metamodel must not be empty!
    JPA 메타모델은 비워져있으면 안된다는 건데,
  1. Spring 컨테이너를 요구하는 test는 가장 기본이 되는 Application 클래스가 항상 로드된다.
  2. Application 클래스에 @EnableJPaAuditing 어노테이션을 달아놓아서 모든 test들이 항상 Jpa관련 bean들을 필요로 하는 상태가 되었다.
  3. 통합 test는 전체 context를 로드하고 Jpa를 포함한 모든 Bean들을 주입받기 때문에 에러가 발생하지 않는다.
  4. 하지만 @WebMvcTest는 Jpa 관련 bean을 전혀 로드하지 않는 단위테스트이기 때문에 에러가 발생하는 것이다.

방법 1. MockBean 추가하기

@MockBean(JpaMetamodelMappingContext.class)
test 클래스에 이 어노테이션을 달아주면 일단 그 test는 오류 없이 실행시킬 수 있으나, 위에서 언급했듯이 모든 test에서 bean을 필요로 하는 상태가 되었기 때문에 모든 test 클래스에 이 어노테이션을 달아주어야 한다.

방법 2. @Configuration 분리하기

@Configuration
@EnableJpaAuditing
public class JpaAuditingConfiguration {  
}

어차피 단위테스트를 계속 만들어야 하기 때문에 configuration을 따로 분리하는게 더 편한 방법인 것 같다.

profile
영차 영차 개발 공부 기록 하기

0개의 댓글