인프런 커뮤니티 질문&답변

lgh8079님의 프로필 이미지
lgh8079

작성한 질문수

스프링 DB 1편 - 데이터 접근 핵심 원리

데이터 접근 예외 직접 만들기

MemberServiceV4Test.java 테스트 실행하는데 오류가 발생합니다.

작성

·

152

0

package hello.jdbc.service; import hello.jdbc.domain.Member; import hello.jdbc.repository.MemberRepository; import hello.jdbc.repository.MemberRepositoryV4_1; import lombok.extern.slf4j.Slf4j; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.aop.support.AopUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.context.annotation.Bean; import javax.sql.DataSource; import java.sql.SQLException; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * 예외 누수 문제 해결 * SQLException 제거 * * MemberRepository 인터페이스 의존 */ @Slf4j @SpringBootTest class MemberServiceV4Test { public static final String MEMBER_A = "memberA"; public static final String MEMBER_B = "memberB"; public static final String MEMBER_EX = "ex"; @Autowired private MemberRepository memberRepository; @Autowired private MemberServiceV4 memberService; @TestConfiguration static class TestConfig { private final DataSource dataSource; public TestConfig(DataSource dataSource) { this.dataSource = dataSource; } @Bean MemberRepository memberRepository() { return new MemberRepositoryV4_1(dataSource); } @Bean MemberServiceV4 memberServiceV4() { return new MemberServiceV4(memberRepository()); } } @AfterEach void after() { memberRepository.delete(MEMBER_A); memberRepository.delete(MEMBER_B); memberRepository.delete(MEMBER_EX); } @Test void AopCheck() { log.info("memberService class={}", memberService.getClass()); log.info("memberRepository class={}", memberRepository.getClass()); Assertions.assertThat(AopUtils.isAopProxy(memberService)).isTrue(); Assertions.assertThat(AopUtils.isAopProxy(memberRepository)).isFalse(); } @Test @DisplayName("정상 이체") void accountTransfer() { //given` Member memberA = new Member(MEMBER_A, 10000); Member memberB = new Member(MEMBER_B, 10000); memberRepository.save(memberA); memberRepository.save(memberB); //when memberService.accountTransfer(memberA.getMemberId(), memberB.getMemberId(), 2000); //then Member findMemberA = memberRepository.findById(memberA.getMemberId()); Member findMemberB = memberRepository.findById(memberB.getMemberId()); assertThat(findMemberA.getMoney()).isEqualTo(8000); assertThat(findMemberB.getMoney()).isEqualTo(12000); } @Test @DisplayName("이체중 예외 발생") void accountTransferEx() { //given Member memberA = new Member(MEMBER_A, 10000); Member memberEx = new Member(MEMBER_EX, 10000); memberRepository.save(memberA); memberRepository.save(memberEx); //when assertThatThrownBy(() -> memberService.accountTransfer(memberA.getMemberId(), memberEx.getMemberId(), 2000)) .isInstanceOf(IllegalStateException.class); //then Member findMemberA = memberRepository.findById(memberA.getMemberId()); Member findMemberB = memberRepository.findById(memberEx.getMemberId()); assertThat(findMemberA.getMoney()).isEqualTo(10000); assertThat(findMemberB.getMoney()).isEqualTo(10000); } }Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended> Task :testMemberServiceV4Test > AopCheck() FAILED org.springframework.jdbc.CannotGetJdbcConnectionException at MemberServiceV4Test.java:64 Caused by: org.h2.jdbc.JdbcSQLNonTransientConnectionException at MemberServiceV4Test.java:64MemberServiceV4Test > 이체중 예외 발생 FAILED org.springframework.jdbc.CannotGetJdbcConnectionException at MemberServiceV4Test.java:102 Caused by: org.h2.jdbc.JdbcSQLNonTransientConnectionException at MemberServiceV4Test.java:102MemberServiceV4Test > 정상 이체 FAILED org.springframework.jdbc.CannotGetJdbcConnectionException at MemberServiceV4Test.java:83 Caused by: org.h2.jdbc.JdbcSQLNonTransientConnectionException at MemberServiceV4Test.java:833 tests completed, 3 failed> Task :test FAILEDFAILURE: Build failed with an exception.* What went wrong:Execution failed for task ':test'.> There were failing tests. See the report at: file:///C:/study/spring-db/jdbc/jdbc/build/reports/tests/test/index.html* Try:> Run with --scan to get full insights.Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.For more on this, please refer to https://docs.gradle.org/8.7/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.BUILD FAILED in 8s4 actionable tasks: 1 executed, 3 up-to-date

스크린샷 2024-04-07 120249.png이런 오류가 발생하는데 어떻게 해결해야 될 지 몰라서 문의 드립니다.

답변 1

0

안녕하세요. lgh8079님, 공식 서포터즈 y2gcoder입니다.

예외만으로 판단하기에는 db 연결 문제로 보입니다!

다음 링크(클릭) 을 참고해보시거나 본 강의 섹션 1. JDBC 이해 > H2 데이터베이스 설정 을 참고하여 다시 H2DB를 설정하고 DB를 킨 상태로 테스트해보시겠습니까?

감사합니다.

lgh8079님의 프로필 이미지
lgh8079

작성한 질문수

질문하기