[트러블슈팅 - Spring] Unable to find a @SpringBootConfiguration

Hocaron·2022년 6월 5일
0

트러블슈팅

목록 보기
3/9

에러 메시지

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

상황

src

+--main

	+--com

		+--demo

			+--DemoApplication.java (@SpringBootApplication)

+--test

	+--com

		+--demo


			+--DemoApplicationTest.java (@SpringBootTest)

		+--demo

			+--others

				+--MyCustomTest.java (@SpringBootTest)

DemoApplicationTest 는 정상 실행 될 것이다. MyCusTomTest 는 위의 에러를 만날 것이다. MyCustomTest 가 찾을 수 있는 스프링부트 설정 클래스가 없기 때문이다.

스프링부트 실행 클래스는 자신의 패키지에서부터 스프링부트 설정 클래스를 찾기 시작하여, 찾을 때 까지 상위 패키지로 계속 찾아나간다.

위의 경우, MyCustomTest 클래스가 실행되면서 스프링부트 설정 클래스를 찾는 순서는 다음과 같다:
1. com.demo.others
2. com.demo
3. com

그런데 위 프로젝트에서 유일한 스프링부트 설정 클래스인 DemoApplication 은 MyCustomTest 의 상위 패키지가 아닌, 같은 레벨의 다른 이름을 가진 패키지에 존재하고, 이 경우 MyCustomTest 는 설정 클래스를 찾을 수 없다.

원인

테스트 클래스를 작성하면서 테스트 클래스 패키지명을 메인 클래스 패키지와 차이가 생긴다.

해결방법

  1. 경로 똑같이 맞추기1 (MyCustomTest 클래스를 com.demo 로 이동)
  2. 경로 똑같이 맞추기2 (com.demo.others 에도 스프링부트 설정 패키지 생성)
  3. 상위 경로에 설정 클래스 하나 두기 (com 또는 com.demo 에 에러 방지용 디폴트 설정 클래스 생성)

References

profile
기록을 통한 성장을

0개의 댓글