spring
[jpa + junit] 슬라이스 테스트 도중 JPA metamodel must not be empty! 해결
sup2is
2020. 9. 1. 14:19
Spring + jpa 환경에서 @WebMvcTest 등등의 슬라이스 테스트 도중
JPA metamodel must not be empty! 라는 에러메세지를 만나게 될 경우 해결 방법이다.
아마 다음과 같이 bootstrapping 클래스인 ***Application의 모습이 다음과 같을 것이다
package me.sup2is.order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@SpringBootApplication
@EnableJpaAuditing
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
}
슬라이스 테스트던 어떤 테스트던 가장 기본이 되는 bootstrapping 클래스는 항상 로딩이된다.
이때 @EnableJpaAuditing 이 붙은 bootstrapping 클래스가 스프링 부트에 의해 로딩되는데 @WebMvcTest같은 테스트 전용 애너테이션은 JPA 관련 빈들을 로딩하지 않기 때문에 일어나느 현상이다.
따라서 다음과 같이 별도의 configuration 클래스를 생성해서 @EnableJpaAuditing 을 붙여주면 해결된다.
@Configuration
@EnableJpaAuditing
public class JpaAuditingConfiguration {}
Spring Boot JPA metamodel must not be empty! when trying to run JUnit / Integration Tests
Am using Spring Boot, JUnit 4 & Mockito in a maven based project to tests my Spring Boot Microservice REST API. So, on startup, the DataInserter class loads data from owner.json and cars.json.
stackoverflow.com