Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is 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.
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.
해당 오류를 발견했다.
그렇지만 해당 오류에서 말하는 데이터 소스는 아무 문제 없이 설정이 잘 되어 있었다.
두번째 오류 내용을 살펴보아 해당 내용을 찾아보니
일반적으로 스프링 부트는 테스트를 위해 H2, HSQLDB, Derby 등의 내장 데이터베이스를 자동으로 설정하고 사용합니다. 그러나 오류 메시지에서 언급된 것처럼 내장 데이터베이스를 사용할 수 없는 경우 발생할 수 있습니다.
필자는 MySQL를 사용 중이였고, 내장 데이터베이스에 대한 설정을 해놓은게 없었다.
(어떻게 보면 내장 데이터베이스의 데이터 소스를 설정하지 않았던거라 데이터 소스에 아무 문제가 없었던건 아니였다.)
그래서 위 오류 내용에서 말하는것과 같이 테스트 클래스에 @AutoConfigureTestDatabase 어노테이션을 설정해주어야 한다
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class Test {
private TestRepository testRepository;
@Autowired
public Test(TestRepository testRepository){
this.testRepository = testRepository;
}
// ..생략
}
'@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)'
해당 어노테이션을 추가하여 내장 데이터베이스의 자동 설정을 비활성화 해주어 문제를 해결했다.
하지만 보통은 내장형 데이터베이스를 사용한다
내장 데이터베이스 없이 MySQL RDS로 테스트를 진행하던 중 insert 테스트 중 꼭 두 번째 이후 테스트가 실패하였다 실패 원인은 중복된 값이 존재하기 때문이었다 실제로 값이 들어있지는 않아서 당황했다
내가 생각하는 이유는 "후속 테스트 사례가 같은 데이터를 사용하는 경우 충돌이 발생할 수 있다" 이다
실제로 테스트 중 테이블을 확인해보아도 데이터가 정상적으로 입력되지 않은 모습을 관찰할 수 있었다
새로운 인스턴스의 데이터베이스를 통해서 테스트를 해야하는데 실제로 RDS의 인스턴스는 인스턴스 초기화를 하지 않는 이상 하나이기 때문에 이런 문제를 일으킨 거 같다.
JPA 테스트는 내장 데이터베이스를 사용하여 테스트를 진행하자!