본문 바로가기

소프트웨어/웹

[Spring boot] JUnit 테스트가 안되는 경우

 

VS Code에서 Spring boot 프로젝트 테스트를 시도하였으나 아래와 같은 오류 메시지가 발생하였습니다.

 

10:46:00.896 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class test.mydemo.DemoApplicationTests] 
10:46:00.901 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate] 
10:46:00.909 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)] 
10:46:00.931 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [test.mydemo.DemoApplicationTests] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper] 
10:46:00.952 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [test.mydemo.DemoApplicationTests], using SpringBootContextLoader 
10:46:00.956 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [test.mydemo.DemoApplicationTests]: class path resource [test/mydemo/DemoApplicationTests-context.xml] does not exist

 

이는 @SpringBootApplication으로 선언된 Class명과 Test Class명이 다른 경우에 이런 현상이 발생한 것으로 보입니다.

 

 

@SpringBootApplication 클래스 명이 DemoApplication이 아닌 MyApplication으로 Bootstrapper의 Auto Configuration을 찾을 수 없었던 거죠..

 

 

 

아래와 같이 Test Class 위에 아래의 어노테이션을 달아주면 문제없이 동작합니다.

/* @SpringBootApplication class를 명시적으로 알려줘야한다. */
@ContextConfiguration(classes = MyApplication.class)
public class DemoApplicationTests {
}

 

 

https://stackoverflow.com/questions/52274066/springrunner-unable-to-detect-configuration/52274408

 

SpringRunner unable to detect configuration

I have a spring-boot application for which am trying to create unit testcases. Below is the code that I am trying to run, I don't have any configuration file that I have (used only annotations) so ...

stackoverflow.com