![](/img/trans.png)
[英]Spring Boot Testing @WebMvcTest for a Controller appears to load other controllers in the context
[英]How to exclude other @Controller from my context when testing using Spring Boot @WebMvcTest
我有多個控制器,我的理解是,通過在@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批注中指定要測試的控制器會將其限制為只能加載該contoller。 但是它試圖加載另一個控制器,並且因為沒有嘲笑它的bean,所以它失敗了。
我缺少什么或者我的理解不正確? 我認為我只需要為測試中的Controller模擬bean。 我還嘗試了excludeFilter專門排除其他Controller的程序包,但這並沒有改變錯誤。
請確保您的測試中的Application.class不包含@ComponentScan批注。 例如,這是您的包裝結構
abc-project
+--pom.xml
+--src
+-- main
+-- com
+-- abc
+-- Application.java
+-- controller
+-- MyController.java
+-- test
+-- com
+-- abc
+-- Application.java
+-- controller
+-- MyControllerTest.java
被測試的Application.java應該看起來類似於此示例,
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
my.package.other.myOtherMapper(可能是Mybatis Mapper文件)丟失或未明確初始化。
據我了解,myOtherService實現類具有未正確映射的Mapper文件。
您可能必須先將它們映射。 如果可能,您可以發布Mapper xml內容。
<context:component-scan base-package="org.example">
<context:exclude-filter type="custom" expression="abc.xyz.MyOtherController"/>
</context:component-scan>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.