Skip to main content Link Menu Expand (external link) Document Search Copy Copied

WebMvcTest 쀑 NoSuchBeanDefinitionException μ—λŸ¬ ν•΄κ²°


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 'hospitalController' defined in file [C:\Users\ikyoo\Desktop\λ©‹μŸμ΄μ‚¬μžμ²˜λŸΌ λ°±μ—”λ“œ 슀쿨\λ³‘μ›μ›Ήμ‚¬μ΄νŠΈν”„λ‘œμ νŠΈ\web\out\production\classes\hospital\web\controller\HospitalController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'hospital.web.service.HospitalService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:90)
	... 72 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'hospital.web.service.HospitalService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791)
	... 90 more


WebMvc ν…ŒμŠ€νŠΈ μ½”λ“œμ—μ„œ μœ„μ™€ 같은 μ—λŸ¬κ°€ κ³„μ†ν•΄μ„œ λ°œμƒν–ˆλ‹€.

근데, μ˜μ•„ν•œμ μ΄ λ‚˜λŠ” UserController λΌλŠ” 파일 ν…ŒμŠ€νŠΈλ₯Ό μ‹€ν–‰μ€‘μ΄μ—ˆλŠ”λ°, 자꾸, hospital κ΄€λ ¨ μ†ŒμŠ€ μ½”λ“œμ—μ„œ μ—λŸ¬λ₯Ό λ°œμƒμ‹œν‚€λŠ” μƒν™©μ΄μ—ˆλ‹€.

해결책은, @WebMvcTest(ν…ŒμŠ€νŠΈν•˜κΈ°λ₯Ό μ›ν•˜λŠ” 클래슀.class) λ₯Ό λͺ…μ‹œν•΄μ£Όμ–΄μ•Ό μ—λŸ¬λ₯Ό λ°œμƒμ‹œν‚€μ§€ μ•ŠλŠ”λ‹€.

@WebMvcTest(UserController.class) 이처럼 ν…ŒμŠ€νŠΈ ν•˜κΈ°λ₯Ό μ›ν•˜λŠ” 클래슀λ₯Ό λͺ…μ‹œν•˜λ‹ˆ μ—λŸ¬κ°€ λ°œμƒν•˜μ§€ μ•Šκ²Œ λ˜μ—ˆλ‹€.