BaseEntity.java
@Getter
@Setter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity {
@CreatedDate
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime updatedAt;
}
SpringApplication.java
@SpringBootApplication
**@EnableJpaAuditing**
public class SsayeonApplication {
public static void main(String[] args) {
SpringApplication.run(SsayeonApplication.class, args);
}
}
@EnableJpaAuditing
을 해줘야 하는데 이렇게 하면 테스트코드를 실행할 때 아래와 같은 에러가 생성된다.
JPA metamodel must not be empty
발생원인은 @WebMvcTest
같은 slice test는 JPA 관련 Bean들을 로드하지 않기 때문에 발생한다.
slice test에서도 jpa관련 bean들을 로드할 수 있게 JpaAuditing
을 분리해주고 @Configuration
을 붙여주자.
@Configuration
@EnableJpaAuditing
public class JpaAuditingConfiguration {
}
그리고 테스트 코드에 @MockBean annotation option에 관련 class를 추가해준다.
@WebMvcTest(UserController.class)
@MockBean(JpaMetamodelMappingContext.class)
class UserControllerTest {