--- tags: jpa, boot, DataJpaTest --- JPA @DataJpaTest 의 필수 의존성 === @DataJpaTest 사용시 별도 옵션을 주지 않으면 embedded DB를 사용 하게 된다. 만약 아무런 DB 의존성 없이 @DataJpaTest를 수행 하려고 하면 다음과 같은 에러가 발생한다. ``` Caused by: java.lang.ClassNotFoundException :org.springframework.dao.DataAccessException ``` > DataAccessException 가 발생. 에러가 났지만 표현할 DataAccessException 클래스 조차 다. 이 Excpetion 클래스는 spring-tx 모듈에 있는데, spring-boot-start-jpa 의존성을 추가 하면 spring-tx가 함께 추가 되니, 추가 해 보자. ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> ``` 다시 run 해서 제대로 된 에러문구를 확인 해 보자. ``` Caused by: java.lang.IllegalStateException : Failed to replace DataSource with an embedded database for tests. If you want an embedded database please put a supported one on the classpath or tune the replace attribute of @AutoConfigureTestDatabase. ``` classpath 에 지원되는 내장형 DB를 추가하는 내용 이다. 참고로 위 에러는 TestDatabaseAutoConfiguration 를 통해 출력 된다. TestDatabaseAutoConfiguration 는, `spring-boot-test-autoconfigure` 의 `/META-INF/spring.factories` 에 의해서 작성된 자동 설정 이다. ![image alt](https://i.imgur.com/E7tzEQd.png) > 내부의 검사 로직까지 확인 한것은 아니지만, h2 가 클래스패스에 없으면 안된다는 등의 assertion 이 있을 것 이다. TestDatabaseAutoConfiguration 가 일을 할수 있도록 다음과 같이 h2 를 추가 해 보자. ``` <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>test</scope> </dependency> ``` > 내장형 DB는 test 스콥에서만 동작하는 것이 적절 하다. 정상적으로 잘 동작 한다.