[JUnit] Error Note : Repository 테스트 중 GenericJDBCException 발생 오류

DEINGVELOP·2022년 9월 7일
0
post-custom-banner

Repository 테스트코드를 마저 작성하는데, 테스트에 자꾸 fail이 뜨며 다음과 같은 오류가 발생하였다.

작성한 코드

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class AnswerRepositoryTest {

    @Autowired
    AnswerRepository answerRepository;

    Member mockMember = Member.builder()
            .username("cheolsu")
            .password("password")
            .nickname("김철수")
            .build();

    Interview mockInterview = Interview.builder()
            .question("스프링이란?")
            .answer("웹 어플리케이션을 만들 수 있는 자바 기반의 웹 프레임워크이다.")
            .subTopic(SubTopic.builder()
                    .topic(Topic.builder()
                            .name("BACKEND")
                            .build())
                    .name("SPRING")
                    .build())
            .build();

    @BeforeEach
    void setUp() {

        MockMemberRepository memberRepository = new MockMemberRepository();
        memberRepository.save(mockMember);

        MockInterviewRepository interviewRepository = new MockInterviewRepository();
        interviewRepository.save(mockInterview);
    }

    @Test
    @DisplayName("답변 저장하기")
    void saveAnswer() {

        // given
        Answer answer = Answer.builder()
                .interview(mockInterview)
                .member(mockMember)
                .content("스프링은 자바 기반 프레임워크입니다.")
                .publicTF(true)
                .build();

        // when
        Answer savedAnswer = answerRepository.save(answer);

        // then
        Assertions.assertThat(answer).isSameAs(savedAnswer);
        Assertions.assertThat(answer.getContent()).isEqualTo(savedAnswer.getContent());
        Assertions.assertThat(answer.isPublicTF()).isEqualTo(savedAnswer.isPublicTF());
        Assertions.assertThat(answerRepository.count()).isEqualTo(1);
    }

    @Test
    @DisplayName("find answer")
    void findAnswer() {
    }
}

발생한 에러

could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement
org.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement
	at app//org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:331)
	at app//org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:233)
	at app//org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:551)
	at app//org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
	at app//org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
	at app//org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:152)
	at app//org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at app//org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:174)
	at app//org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at app//org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
	at app//org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at app//org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
	at app//com.sun.proxy.$Proxy131.save(Unknown Source)
    ...(이하 생략)

원인

데이터베이스 테이블의 필드명을 한 번 바꿨었는데, 그때 생성됐던 테이블이 정돈이 안 되어서 Not Null 값이어야 하는 테이블 필드에 Null값이 입력되어서 발생한 문제였다.


해결

해당 테이블을 DROP하고 다시 CREATE 하니 해결되었다.

post-custom-banner

0개의 댓글