jnk1m
Foliage IT
jnk1m
전체 방문자
오늘
어제
  • 분류 전체보기 (209)
    • Today I Learned (34)
    • Java (47)
    • Database (15)
    • [NHN Academy] (27)
    • Spring (47)
    • HTML + CSS + JavaScript (11)
    • JSP (3)
    • Node.js (10)
    • React Native (2)
    • 기타 (8)
    • 스크랩 (5)

인기 글

최근 글

티스토리

hELLO · Designed By 정상우.
글쓰기 / 관리자
jnk1m

Foliage IT

Spring

[에러] @DataJpaTest 할 때 UnsatisfiedDependencyException

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" 속성을 줘서 설정을 다르게 해줄 수 있다.

    'Spring' 카테고리의 다른 글
    • RestTemplate get 비교 시 객체 처리
    • Spring Boot에서 RestTemplate 빈 등록을 해야할까?
    • [에러] Interface 기반 DTO Projection NULL 에러 원인과 해결 방법
    • [스프링 시큐리티] 사용자 관리 UserDetails, UserDetailsService, UserDetailsManager

    티스토리툴바