I have multiple controllers and my understanding is that by specifying one controller in @WebMvcTest that other controllers wouldn't be loaded into context. From the docs
controllers - Specifies the controllers to test. May be left blank if all @Controller beans should be added to the application context.
My first Controller
@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);
}
}
My other controller
@Controller
public class MyOtherController {
@Autowired
private MyOtherService myOtherService;
etc...
}
My Test for my Controller
@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());
}
}
But when I run this test, it fails trying to load the bean MyOtherService from MyOtherContoller when loading the context.
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)}
I thought specifying the controller to test in the WebMvcTest annotation would limit it to the only loading that contoller. But its trying to load the other controller and because its beans aren't mocked it fails.
What am I missing or is my understanding incorrect? I think I should only have to mock beans for the Controller under test. I've also tried an excludeFilter to specifically exclude the package for the other Controller but that didn't change the error.