[Spring] Failed to load ApplicationContext 에러 해결

오영선·2022년 7월 18일
4

Junit5로 테스트를 진행하던 도중 Failed to load ApplicationContext
java.lang.IllegalStateException: Failed to load ApplicationContext에러가 발생해 해결 방법을 적어보았다.

Failed to load ApplicationContext
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unable to create index (cratedAt) on table article_comment: 
**database column 'cratedAt' not found.**
Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types)
	at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804)
	at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620)
	

계속 @Autowired에 문제가 있다고 생각해서 구글링, 여러 블로그를 참고해도 아무리 해결이 되지 않았다.. 또한 Junit5를 사용하고 있기 때문에 @WebAppConfiguration 도 도움이 되지 않았다.
그러던 차에
https://junior-datalist.tistory.com/264?category=911489
이 블로그 글을 읽게 되었고 내 문제가 junitTest뿐만 아니라 MainApplicationTest에서도 똑같은 에러가 나는 것을 확인할 수 있었다.

그리고 causedBy에서 에러 상황을 꼼꼼히 읽어보니 entity에서 컬럼을 만들던 도중 오타가 있던 것을 발견하지 못하여 일어난 일이었다.

@Table(indexes =  {
        @Index(columnList = "content"),
        @Index(columnList = "cratedAt"), <-오타가 났었다.
        @Index(columnList = "createdBy")
})

오타를 완전히 없앨 순 없겠지만 오류코드를 꼼꼼히 읽는 연습을 해야겠다.

+)

Failed to load ApplicationContext
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.UncategorizedScriptException: Failed to execute database script from resource [URL [file:/D:/github/fastcampus/build/resources/main/data.sql]]; nested exception is java.lang.IllegalArgumentException: 'script' must not be null or empty

dataSourceScriptDatabaseInitializer에서 오류가 나는 경우 :
data.sql 에 잘못된 컬럼이 들어가지 않았는지. 또는 null, empty상태가 아닌지 확인 해야한다.(반드시 한 개 이상의 data가 있어야함)

0개의 댓글