Ocean_
꾸준한 프로그래밍
Ocean_
전체 방문자
오늘
어제
  • 분류 전체보기 (174)
    • About Me (4)
    • 우아한테크코스 (27)
    • C & LINUX (50)
    • Operating System (12)
    • Database (25)
    • Computer Vision (0)
    • Problem Solving (0)
      • Programmers (0)
      • BOJ (0)
    • 코코코딩공부 (44)
      • Spring (12)
      • JAVA (14)
      • 디자인 패턴 (4)
      • 책 읽기 (4)

블로그 메뉴

  • 홈
  • 방명록

공지사항

인기 글

태그

  • 데이터 조작어
  • BOJ
  • C
  • 1259
  • 백준
  • Operating System
  • OperatingSystem
  • 리눅스
  • SIGINT
  • 트랜잭션
  • 프로세스
  • DB
  • OS
  • 자바
  • 블랙잭
  • 원시값 포장
  • 우아한테크코스5기
  • 정규화
  • 우테코5기
  • 인덱스
  • signal
  • 우아한형제들
  • 운영체제
  • 우테코 체스
  • Spring
  • Linux
  • C++
  • 우테코
  • 우아한테크코스
  • bean

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Ocean_

꾸준한 프로그래밍

코코코딩공부/Spring

[Spring] 테스트 DB Failed to load ApplicationContext 에러

2023. 4. 18. 11:00

문제

스프링 미션을 진행하던 중 단위 테스트는 잘 수행되었으나 통합테스트가 에러가 났다.

java.lang.IllegalStateException: Failed to load ApplicationContext

Failed to load ApplicationContext
java.lang.IllegalStateException: Failed to load ApplicationContext
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:98)
	~~~
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webController' defined in file [/Users/donghae/Desktop/study/jwp-racingcar/build/classes/java/main/racingcar/controller/WebController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'gameService' defined in file [/Users/donghae/Desktop/study/jwp-racingcar/build/classes/java/main/racingcar/service/GameService.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inMemoryCarDao' defined in file [/Users/donghae/Desktop/study/jwp-racingcar/build/classes/java/main/racingcar/dao/InMemoryCarDao.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is 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.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/Users/donghae/Desktop/study/jwp-racingcar/build/resources/main/data.sql]: CREATE TABLE PLAY_RESULT ( id INT NOT NULL AUTO_INCREMENT, trial_count INT NOT NULL, winners VARCHAR(50) NOT NULL, created_at DATETIME NOT NULL default current_timestamp, PRIMARY KEY (id) ); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "PLAY_RESULT" already exists; SQL statement:
CREATE TABLE PLAY_RESULT ( id INT NOT NULL AUTO_INCREMENT, trial_count INT NOT NULL, winners VARCHAR(50) NOT NULL, created_at DATETIME NOT NULL default current_timestamp, PRIMARY KEY (id) ) [42101-214]
	~~~
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'gameService' defined in file [/Users/donghae/Desktop/study/jwp-racingcar/build/classes/java/main/racingcar/service/GameService.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inMemoryCarDao' defined in file [/Users/donghae/Desktop/study/jwp-racingcar/build/classes/java/main/racingcar/dao/InMemoryCarDao.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is 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.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/Users/donghae/Desktop/study/jwp-racingcar/build/resources/main/data.sql]: CREATE TABLE PLAY_RESULT ( id INT NOT NULL AUTO_INCREMENT, trial_count INT NOT NULL, winners VARCHAR(50) NOT NULL, created_at DATETIME NOT NULL default current_timestamp, PRIMARY KEY (id) ); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "PLAY_RESULT" already exists; SQL statement:
CREATE TABLE PLAY_RESULT ( id INT NOT NULL AUTO_INCREMENT, trial_count INT NOT NULL, winners VARCHAR(50) NOT NULL, created_at DATETIME NOT NULL default current_timestamp, PRIMARY KEY (id) ) [42101-214]
	~~~
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inMemoryCarDao' defined in file [/Users/donghae/Desktop/study/jwp-racingcar/build/classes/java/main/racingcar/dao/InMemoryCarDao.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is 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.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/Users/donghae/Desktop/study/jwp-racingcar/build/resources/main/data.sql]: CREATE TABLE PLAY_RESULT ( id INT NOT NULL AUTO_INCREMENT, trial_count INT NOT NULL, winners VARCHAR(50) NOT NULL, created_at DATETIME NOT NULL default current_timestamp, PRIMARY KEY (id) ); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "PLAY_RESULT" already exists; SQL statement:
CREATE TABLE PLAY_RESULT ( id INT NOT NULL AUTO_INCREMENT, trial_count INT NOT NULL, winners VARCHAR(50) NOT NULL, created_at DATETIME NOT NULL default current_timestamp, PRIMARY KEY (id) ) [42101-214]
	~~~
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.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/Users/donghae/Desktop/study/jwp-racingcar/build/resources/main/data.sql]: CREATE TABLE PLAY_RESULT ( id INT NOT NULL AUTO_INCREMENT, trial_count INT NOT NULL, winners VARCHAR(50) NOT NULL, created_at DATETIME NOT NULL default current_timestamp, PRIMARY KEY (id) ); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "PLAY_RESULT" already exists; SQL statement:
CREATE TABLE PLAY_RESULT ( id INT NOT NULL AUTO_INCREMENT, trial_count INT NOT NULL, winners VARCHAR(50) NOT NULL, created_at DATETIME NOT NULL default current_timestamp, PRIMARY KEY (id) ) [42101-214]
	~~~
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "PLAY_RESULT" already exists; SQL statement:
CREATE TABLE PLAY_RESULT ( id INT NOT NULL AUTO_INCREMENT, trial_count INT NOT NULL, winners VARCHAR(50) NOT NULL, created_at DATETIME NOT NULL default current_timestamp, PRIMARY KEY (id) ) [42101-214]
	~~~

처음에는 에러가 엄청 길다 보니 bean을 생성하지 못하네? 왜 못하지 ? 라는 생각을 하였다.

하지만 잘 살펴보니 아래와 같은 에러를 보았다.

Table "PLAY_RESULT" already exists;

원인

여러 통합 테스트의 @SpringBootTest 는 여러개의 ApplicationContext를 생성한다.  나의 테스트 에서는 이 각각의 SpringBootTest가 있었는데 이럴 경우 RandomPort가 생성되어 웹환경을 구축하고, None으로 웹환경을 구축하기에 각각의 ApplicationContext이 생성된다.

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)

그 경우 나처럼 db를 사용중이면 resource에 있는 data.sql로 db를 생성초기화 한다. 

하지만 JVM에는 이미 db가 올라가 있고 그 밑단에서 새롭게 db를 생성하려고 하니 에러가 발생하는 것이다 !

 

나는 분명 테스트코드에서 BeforeEach로 계속 테이블을 drop해주었는데 왜 안되지 ? 라는 의문을 계속 가졌다.

이 의문에 대한 해결은 beforeEach가 돌기도 전에 ApplicationContext생성될때 에러를 띄우기에 작동조차 하지 않았던 것이다 ....


해결

CREATE TABLE IF NOT EXISTS CAR_RESULT (
);

위와 같이 Sql문에서 재생성 불가하게 막았다.

'코코코딩공부 > Spring' 카테고리의 다른 글

[Spring] Dispatcher Servlet이란 ? & 미션 에서 찾아보기  (0) 2023.04.30
[Spring] Servlet 이란 ?  (0) 2023.04.30
[Spring] 어떤 객체를 빈으로 등록해야 할까 ?  (1) 2023.04.23
[Spring] Bean 이란 ?  (0) 2023.04.23
[Architecture] Layered Architecture  (3) 2023.04.15
    '코코코딩공부/Spring' 카테고리의 다른 글
    • [Spring] Servlet 이란 ?
    • [Spring] 어떤 객체를 빈으로 등록해야 할까 ?
    • [Spring] Bean 이란 ?
    • [Architecture] Layered Architecture
    Ocean_
    Ocean_
    dongVeloper

    티스토리툴바