Could not detect default configuration classes for test class

# 증상

spring unit 테스트가 실행 안되고 아래 오류를 보이며 종료. 프로젝트는 멀티 모듈로 구성

Process finished with exit code 255

error code 255는 보통 실행을 위한 파일을 찾을 수 없다는 의미. 무슨 파일이 없지?

# 원인

  • 프로그램이 main 함수를 찾아 실행하듯, spring test 시작시 패키지를 순회하며 @SpringBootApplication 혹은 @SpringBootConfiguration을 먼저 찾는다. 문서 참고 (opens new window)
  • 모듈로 구성된 프로젝트일 경우 테스트를 실행하는 모듈에 저 어노테이션이 없을 것이다.

# 해결

  • Configuration class를 생성해서 @SpringBootConfiguration를 추가.
  • @DataJpaTest일 경우, 빠져있는 자동설정 몇가지를 추가해 주자.
@SpringBootConfiguration // 설정 추가
@EnableJpaRepositories // jpa test시 jpa 설정 활성화
public class JpaTestConfiguration {

    @Bean
    public JPAQueryFactory jpaQueryFactory(EntityManager entityManager) {
        return new JPAQueryFactory(entityManager);
    }

}
1
2
3
4
5
6
7
8
9
10
@DataJpaTest
@EntityScan("com.test.*") // 수동 설정 필요.
class FooRepositoryTest {
  ...
}
1
2
3
4
5