Back-end/TroubleShooting

[Spring Boot] ๋ฉ€ํ‹ฐ๋ชจ๋“ˆ 'Unable to find a @SpringBootConfiguration'

์„œ์ฑ„๋ฆฌ 2024. 7. 30. 19:57

๐ŸŒฑ ๋ฌธ์ œ ์ƒํ™ฉ

๋ฉ€ํ‹ฐ๋ชจ๋“ˆ ํ”„๋กœ์ ํŠธ ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๋˜ ์ค‘ ๋„๋ฉ”์ธ ๋ชจ๋“ˆ์—์„œ @DataJpaTest๋ฅผ ์‚ฌ์šฉํ•˜๋‹ˆ ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

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

 

@DataJpaTest๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, ์Šคํ”„๋ง ๋ถ€ํŠธ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์–ด๋–ป๊ฒŒ ์‹œ์ž‘๋ ์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” @SpringBootApplication ๋˜๋Š” @SpringBootConfiguration์ด ๋ถ™์€ ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ํ•˜์œ„ ํŒจํ‚ค์ง€๋ฅผ @ComponentScan ๋ฐ @EntityScan ๋“ฑ์˜ ์ž‘์—…์„ ํ†ตํ•ด ์Šค์บ”ํ•˜๊ณ , ๋นˆ(bean)์„ ๋“ฑ๋กํ•˜์—ฌ ์„ค์ •์„ ์ ์šฉํ•˜๊ฒŒ ๋œ๋‹ค.

 

๊ทธ๋Ÿฌ๋‚˜ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฉ€ํ‹ฐ๋ชจ๋“ˆ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ชจ๋“ˆ ์™ธ์˜ ๋‹ค๋ฅธ ๋ชจ๋“ˆ์—์„œ๋Š” @SpringBootApplication ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๋Š”๋‹ค. ์ด๋กœ ์ธํ•ด ๋„๋ฉ”์ธ ๋ชจ๋“ˆ์—์„œ @DataJpaTest์™€ ๊ฐ™์€ ์Šคํ”„๋ง ๋ถ€ํŠธ ๊ธฐ๋ฐ˜ ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ ค๊ณ  ํ•˜๋ฉด, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์—†์–ด IllegalStateException: Unable to find a @SpringBootConfiguration ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

๐ŸŒฑ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ

ํ…Œ์ŠคํŠธ ๋ ˆ๋ฒจ ์ตœ์ƒ์œ„ ํŒจํ‚ค์ง€์— @SpringBootApplication ํด๋ž˜์Šค ๋งŒ๋“ค๊ธฐ

@SpringBootApplication
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}

์œ„์—์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ์Šคํ”„๋ง ๋ถ€ํŠธ๋Š” @SpringBootApplication์ด ๋ถ™์€ ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ์ž๋™ ์„ค์ •๊ณผ ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์„ ํ•˜์ง€๋งŒ ๋„๋ฉ”์ธ ๋ชจ๋“ˆ์—๋Š” @SpringBootApplication ํด๋ž˜์Šค๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ๋ณธ์ ์ธ ํŒจํ‚ค์ง€ ์Šค์บ”์„ ํ•  ์ˆ˜ ์—†๋‹ค.

 

๋”ฐ๋ผ์„œ ํ…Œ์ŠคํŠธ ๋ ˆ๋ฒจ ์ตœ์ƒ์œ„ ํŒจํ‚ค์ง€์— @SpringBootApplication ์–ด๋…ธํ…Œ์ด์…˜์ด ์žˆ๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๋ฉด, ์Šคํ”„๋ง ๋ถ€ํŠธ๋Š” ์ด ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

 

๐Ÿ˜… ๋„๋ฉ”์ธ DataJpaTest๋ฅผ ํ•˜๋ฉด์„œ ์ถ”๊ฐ€์ ์œผ๋กœ ์„ค์ •ํ•œ ๊ฒƒ๋“ค

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.querydsl.jpa.impl.JPAQueryFactory' available: expected at least 1 bean which qualifies as autowire candidate.

QueryDSL์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ @TestConfiguration๋ฅผ ํ†ตํ•ด ๊ด€๋ จ ๋นˆ ์ถ”๊ฐ€๋กœ ์„ค์ •ํ•œ ํ›„ ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค์—์„œ ํ•ด๋‹น config ํด๋ž˜์Šค import

@TestConfiguration
public class TestConfig {

    @PersistenceContext
    private EntityManager entityManager;

    @Bean
    public JPAQueryFactory jpaQueryFactory() {
        return new JPAQueryFactory(entityManager);
    }
}
@Import(TestConfig.class)
@DataJpaTest
class ...Test { ... }

IllegalStateException: Cannot load driver class: org.h2.Driver

domain ๋ชจ๋“ˆ์˜ build.gradle ํŒŒ์ผ์— h2 testImplementation ์ถ”๊ฐ€

dependencies {
    ...
    
    testImplementation 'com.h2database:h2'
}