bootrun 은 성공하는데 test 는 실패...
Description:
Parameter 0 of constructor in com.example.ExampleController required a bean of type 'com.example.ExampleService' that could not be found.
Action:
Consider defining a bean of type 'com.example.ExampleService' in your configuration.
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'exampleController' defined in file [D:\work\Api\build\classes\java\main\com\example\ExampleController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.ExampleService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.ExampleService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
원인은 @WebMvcTest 가 @Service / @Repository 빈을 생성하지 못하는데 있었다.
@WebMvcTest
- @SpringBootTest 와 달리 전체 자동 구성이 비활성화되고 MVC 테스트 구성만 로드한다.
- @Component, @Service, @Repository 등의 빈은 로드하지 않으므로, 필요할 경우 @MockBean 이나 @Import 를 사용한다.
- JUnit4 를 사용할 때 @RunWith(SpringRunner.class)와 함께 사용한다.
@WebMvcTest(ExampleController.class)
public class ExampleControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ExampleService exampleService;
...
}
WRITTEN BY
- 손가락귀신
정신 못차리면, 벌 받는다.