@무린 답변 감사드립니다 하지만 위와같이 모두 따라했으나 오류는 멈추지 않아요 ㅡㅜ Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Failed to initialize dependency 'dataSourceScriptDatabaseInitializer' of LoadTimeWeaverAware bean 'entityManagerFactory': 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: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Cannot load driver class: org.h2.Driver
애플리케이션 실행이 안되는 것은 여러가지 원인이 있을 수 있습니다.
1. findBySubject가 null을 반환하는 경우
Question q = this.questionRepository.findBySubject("sbb가 무엇인가요?"); assertEquals(1, q.getId());
이 코드에서 findBySubject("sbb가 무엇인가요?")가 null을 반환하면 q.getId()를 호출할 때 NullPointerException이 발생할 수 있습니다.
아래와 같이 미리 데이터를 삽입하는 코드를 추가해서 확인해보시기 바랍니다.
@Test void testJpa() { Question q1 = new Question(); q1.setSubject("sbb가 무엇인가요?"); q1.setContent("Spring Boot 게시판 프로젝트입니다."); this.questionRepository.save(q1); Question q = this.questionRepository.findBySubject("sbb가 무엇인가요?"); assertEquals(q1.getId(), q.getId()); // 기존 1 대신 저장된 ID 비교 }
2. QuestionRepository 인터페이스 확인
findBySubject 메서드가 제대로 정의되어 있는지 확인하세요.
메소드 이름이 정확한지도 확인해보시기 바랍니다.
3. application.properties 데이터베이스 설정 확인
Spring Boot가 데이터베이스를 연결하지 못하면 ApplicationContext를 시작할 수 없습니다.
데이터베이스 설정도 확인해보시기 바랍니다.
4. 디버그 모드 활성화해서 확인해보기
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2025-02-13T00:25:08.715+09:00 ERROR 7396 --- [ main] o.s.boot.SpringApplication : Application run failed
위 에러를 보면 debug 모드가 활성화되어있지 않습니다. application.properties에 아래와 같이 설정을 추가하여 실행해보면 더 자세한 에러 로그를 볼 수 있습니다.
그럼 확인해보고 안되시면 또 질문주세요!