我有多個控制器,我的理解是通過在@WebMvcTest中指定一個控制器,其他控制器不會加載到上下文中。來自docs如何在使用Spring Boot進行測試時從我的上下文中排除其他@Controller @WebMvcTest
控制器 - 指定要測試的控制器。如果應將所有@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 whe的bean MyOtherService n加載上下文。
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沒有被模擬,所以它失敗了。
我錯過了什麼或者我的理解不正確?我想我應該只能爲受測試的控制器模擬bean。我也嘗試過一個excludeFilter特別排除其他控制器的軟件包,但沒有更改錯誤。
什麼是您的應用程序類的樣子? –