我有多个控制器,我理解@WebMvcTest中指定一个控制器后其他控制器不会被加载到上下文中。关于此,请参考文档
controllers - 指定要测试的控制器。如果所有@Controller bean都应添加到应用程序上下文中,则可以留空。
我的第一个控制器
@Controller
public class MyController {
@Autowired
private MyService myService;
private final Logger logger = Logger.getLogger(this.getClass());
@RequestMapping(value = "/", method = RequestMethod.GET)
public @ResponseBody ResponseEntity<String> index() {
try {
myService.get();
return new ResponseEntity<String>(HttpStatus.OK);
} catch (Exception e) {
logger.error(e);
e.printStackTrace();
}
return new ResponseEntity<String>("REQUEST FAILED", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
我的另一个控制器
@Controller
public class MyOtherController {
@Autowired
private MyOtherService myOtherService;
etc...
}
我的控制器测试
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = { MyController.class }, secure = false)
@ActiveProfiles({ "test" })
public class MyControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
MyService myService;
@Test
public void testBaseReq() throws Exception {
Testing dummyData = new Testing();
dummyData.setData("testing");
when(myService.get(anyInt())).thenReturn(dummyData);
this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk());
}
}
但是当我运行这个测试时,在加载上下文时,它尝试从MyOtherContoller中加载bean MyOtherService而失败。
2017-09-28 11:50:11.687 DEBUG 16552 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'myOtherController'
2017-09-28 11:50:11.687 DEBUG 16552 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating instance of bean 'myOtherController'
2017-09-28 11:50:11.687 DEBUG 16552 --- [ main] o.s.b.f.annotation.InjectionMetadata : Registered injected element on class [my.package.other.myOtherController]: AutowiredFieldElement for private my.package.other.myOtherService my.package.other.myOtherController.myOtherService
2017-09-28 11:50:11.687 DEBUG 16552 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Eagerly caching bean 'myOtherController' to allow for resolving potential circular references
2017-09-28 11:50:11.687 DEBUG 16552 --- [ main] o.s.b.f.annotation.InjectionMetadata : Processing injected element of bean 'myOtherController': AutowiredFieldElement for private my.package.other.myOtherService my.package.other.myOtherController.myOtherService
2017-09-28 11:50:11.688 DEBUG 16552 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'myOtherService'
2017-09-28 11:50:11.688 DEBUG 16552 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating instance of bean 'myOtherService'
2017-09-28 11:50:11.689 DEBUG 16552 --- [ main] o.s.b.f.annotation.InjectionMetadata : Registered injected element on class [my.package.other.myOtherService]: AutowiredFieldElement for private my.package.other.myOtherMapper my.package.other.myOtherService.myOtherMapper
2017-09-28 11:50:11.689 DEBUG 16552 --- [ main] o.s.b.f.annotation.InjectionMetadata : Registered injected element on class [my.package.other.myOtherService]: AutowiredFieldElement for private ie.aib.services.coredemo.FinancialsRegionService my.package.other.myOtherService.financialsRegionService
2017-09-28 11:50:11.689 DEBUG 16552 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Eagerly caching bean 'myOtherService' to allow for resolving potential circular references
2017-09-28 11:50:11.689 DEBUG 16552 --- [ main] o.s.b.f.annotation.InjectionMetadata : Processing injected element of bean 'myOtherService': AutowiredFieldElement for private my.package.other.myOtherMapper my.package.other.myOtherService.myOtherMapper
2017-09-28 11:50:11.690 WARN 16552 --- [ main] o.s.w.c.s.GenericWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myOtherController': Unsatisfied dependency expressed through field 'myOtherService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myOtherService': Unsatisfied dependency expressed through field 'myOtherMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'my.package.other.myOtherMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我以为在WebMvcTest注解中指定要测试的控制器会限制它只加载该控制器。但它尝试加载其他控制器,因为它们的bean没有被mock,所以失败了。
我错了吗?还是我理解有误?我认为我只需要mock正在测试的控制器的beans。我也尝试过使用excludeFilter来特别排除其他控制器的包,但这并没有改变错误。