Spring boot 如何在测试中创建多个MockMvc对象?

Spring boot 如何在测试中创建多个MockMvc对象?,spring-boot,testing,Spring Boot,Testing,我有一个这样注释的Spring测试 @AutoConfigureMockMvc @WebMvcTest public class StepDefinitions { @Autowired private MockMvc mockMvc; 在测试中,我使用mockMvc.perform(post(“/api/… 我通过调用等效的mockMvc.perform(get(“/api/…)来验证它 但是,我使用的是分布式数据库,现在需要检查记录是否存储在另一个数据库中,并向另一个后端发出ge

我有一个这样注释的Spring测试

@AutoConfigureMockMvc
@WebMvcTest
public class StepDefinitions {

  @Autowired
  private MockMvc mockMvc;
在测试中,我使用
mockMvc.perform(post(“/api/…

我通过调用等效的
mockMvc.perform(get(“/api/…
)来验证它

但是,我使用的是分布式数据库,现在需要检查记录是否存储在另一个数据库中,并向另一个后端发出get请求

如何创建MockMvc的第二个实例,该实例链接到一组不同的后端控制器和服务,以便在同一测试中使用它们

编辑:

一个选项是不使用
AutoConfigureMockMvc
,而是使用
限定符定义的多个
MockMvc
bean来指定我的上下文配置

@ContextConfiguration(classes = TestConfiguration.class)
@WebMvcTest
public class StepDefinitions {

  @Autowired
  @Qualifier("firstMockMvc")
  private MockMvc firstMockMvc;
测试配置

@Bean
@Qualifier("firstMockMvc")
public MockMvc firstMockMvc(@Qualifier("firstController") MyController myController) {
  return MockMvcBuilders.standaloneSetup(myController).build();
}


@Bean
@Qualifier("secondMockMvc")
public MockMvc secondMockMvc(@Qualifier("secondController") MyController myController) {
  return MockMvcBuilders.standaloneSetup(myController).build();
}
但是,这会导致
WebMvcTest
配置出现问题

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'firstController' method 
public org.springframework.http.ResponseEntity<java.util.List<com.my.app.web.dto.DataDTO>> com.my.app.web.rpc.MyController.getEmployees()
to {GET /api/employees/employees, produces [application/json]}: There is already 'firstController' bean method
public org.springframework.http.ResponseEntity<java.util.List<com.my.app.web.dto.DataDTO>> com.my.app.web.rpc.MyController.getEmployees() mapped.
原因:org.springframework.beans.factory.BeanCreationException:创建名为“requestMappingHandlerMapping”的bean时出错,该名称在类路径资源[org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$enablewbmvcconfiguration.class]中定义:调用init方法失败;嵌套异常为java.lang.IllegalStateException:映射不明确。无法映射“firstController”方法
public org.springframework.http.ResponseEntity.com.my.app.web.rpc.MyController.getEmployees()
要{GET/api/employees/employees,生成[application/json]}:已经有了“firstController”bean方法
public org.springframework.http.ResponseEntity com.my.app.web.rpc.MyController.getEmployees()已映射。
这似乎意味着我必须手动连接整个web配置

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'firstController' method 
public org.springframework.http.ResponseEntity<java.util.List<com.my.app.web.dto.DataDTO>> com.my.app.web.rpc.MyController.getEmployees()
to {GET /api/employees/employees, produces [application/json]}: There is already 'firstController' bean method
public org.springframework.http.ResponseEntity<java.util.List<com.my.app.web.dto.DataDTO>> com.my.app.web.rpc.MyController.getEmployees() mapped.

有没有更简单的方法可以做到这一点?

使用
MockMvcBuilders.standaloneSetup
创建mockmvcbean并添加编辑中提到的
@限定符
注释是解决方案的一部分

另一部分是删除
@WebMvcTest
@ComponentScan
,这样主包中的控制器bean就不会被加载,从而导致
不明确的映射
错误

我不得不为我的控制器和服务添加bean,但不必为底层springbean(除了MockMvc)做任何额外的连接