Spring

[에러] @DataJpaTest 할 때 UnsatisfiedDependencyException

jnk1m 2022. 12. 14. 14:03
@DataJpaTest
class AccountRepositoryTest {
    @Autowired
    AccountRepository accountRepository;

    @Autowired
    TestEntityManager testEntityManager;

    @Test
    void testFindName(){
        //given
        String axl = "Axl Rose";
        Account account = new Account(11L, axl, "10_000");
        testEntityManager.persist(account);

        //when
        List<Account> actual = accountRepository.findByNumber(axl);

        //then
        assertThat(actual).hasSize(1);
        assertThat(actual.get(0)).isEqualTo(account);
    	}
    }
UnsatisfiedDependencyException: 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.

JPA 테스트를 돌리면 기본적으로 인메모리디비를 찾는다. 하지만 현재 설정은 인메모리디비가 없어서 데이터 소스를 찾지 못하고 있다.

 

아래와 같이 h2 인 메모리 디비 의존성을 추가해주면 작동한다.

 

<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<scope>test</scope>
</dependency>

하지만 ❗️ ❗️ h2는 테스트 돌리기 위해서 필요한데 굳이 pom.xml에 추가를 해야할까?!

이 부분은 yml 파일에 "test" 속성을 줘서 설정을 다르게 해줄 수 있다.