Spring integration 在Spring REST文档测试中模拟@MessagingGateway

Spring integration 在Spring REST文档测试中模拟@MessagingGateway,spring-integration,spring-restdocs,Spring Integration,Spring Restdocs,我有一个@RestController,它使用了@MessagingGateway,我想知道SpringREST文档是否内置了模拟Spring集成组件的支持。利用Spring REST文档为该场景生成文档的最佳方法是什么(即,模拟@MessagingGateway)的最佳支持方式是什么?如果您的意思是要针对注入了模拟接口的控制器运行REST文档,那么类似的方法应该可以工作 @Autowired private MyController controller; @Test public void

我有一个
@RestController
,它使用了
@MessagingGateway
,我想知道SpringREST文档是否内置了模拟Spring集成组件的支持。利用Spring REST文档为该场景生成文档的最佳方法是什么(即,模拟
@MessagingGateway
)的最佳支持方式是什么?

如果您的意思是要针对注入了模拟接口的控制器运行REST文档,那么类似的方法应该可以工作

@Autowired
private MyController controller;

@Test
public void restDocsWithMockGateway() {
    MyGateway gate = mock(MyGateway.class);
    willReturn(new Bar("xxx")).given(gate).foo(any(Foo.class));
    this.controller.setMyGateway(gate); // replace the SI implementation with the mock

    // now do mockmvc stuff with REST Docs

}
假定

@MessagingGateway
public interface MyGateway {

    Bar foo(Foo foo);

}
然而,模仿网关实际上与REST文档无关


如果你不是这个意思,请扩大你的问题。

你的意思不清楚。REST文档用于帮助记录REST API<代码>MessagingGateway可以在REST方法中使用,但不被视为API的一部分。您可以使用任何标准模拟工具(Mockito等)模拟网关方法。也许我在你的问题中遗漏了什么;如果是这样的话,请编辑您的问题,用更多的细节来展开它,也许还可以用一个例子来说明您的意思。当使用RestTemplate调用远程API时,我正在寻找与
@AutoConfigureStubRunner
等效的方法。您可能还记得,在我的另一篇文章中,您曾帮助我()使用RestTemplate调用远程API,在修改所有后端服务以利用Spring Cloud Stream异步处理请求之后,我使用了
@MessagingGateway
。我有前端REST API,我仍然希望为其生成文档。您似乎将REST(API)文档与REST控制器内部合并在一起-看看我的答案是否有帮助。由于
@MessagingGatewau
只是一个接口,因此模拟它是很简单的。如果您使用自动布线,只需从测试中省略
@IntegrationComponentScan
,而是添加一个模拟
@Bean