-
[JPA] test 시 @RequiredArgsConstructor 사용 불가자바/JPA 2024. 1. 21. 22:25
| 문제점
평소 lombok의 Annotation인 @RequiredArgsConstructor를 통해 @Autowired로 의존성을 주입하지 않고 private final로 생성자 의존성 주입을 해왔습니다. 이는 Spring 컨테이너를 통해 의존성을 주입시켜주는 방식으로 사용이 가능했으나, Spring에서 Test를 진행할 경우에는 Spring 컨테이너를 사용하지 않고 JUnit 프레임워크를 사용해 lombok이 동작하지 않습니다.
@SpringBootTest @RequiredArgsConstructor class VideoTest { private final VideoRepository videoRepository; private final VideoPostService videoPostService; @Test void createVideo() { ....
| 해결법
@RequiredArgsConstructor가 아닌 @Autorwired를 통해 의존성 주입을 받아 test를 진행하면 됩니다.
@SpringBootTest class VideoTest { @Autowired public VideoRepository videoRepository; @Autowired public VideoPostService videoPostService; @Test void createVideo() { ...
'자바 > JPA' 카테고리의 다른 글
[JPA] jakarta.persistence @Annotation 정리 (0) 2024.01.18 [JPA] Lombok 이란? (0) 2024.01.06 [JPA] Persistence(영속성) 정리 (0) 2024.01.02 [JPA] JPA란? (0) 2023.12.18 [JPA] console에 query parameter 값 표시 with.springboot (1) 2023.11.29