1

I have a @RestController that makes use of a @MessagingGateway, and I am wondering whether Spring REST Docs has any support built in for mocking Spring Integration components. What is the best approach for leveraging Spring REST Docs to generate documentation for this scenario (i.e., what is the best supported way to mock the @MessagingGateway)?

Keith Bennett
  • 733
  • 11
  • 25
  • It's not clear what you mean. REST Docs is to help document a REST API. `MessagingGateway` might be used __within__ a REST method but would not be considered part of the API. You can mock the gateway method with any standard mocking tools (Mockito etc). Perhaps I am missing something in your question; if so, please edit your question to expand it with more details and perhaps an example of what you mean. – Gary Russell Dec 29 '17 at 18:28
  • I was looking for the equivalent of `@AutoConfigureStubRunner` when a RestTemplate is used to invoke a remote API. You may remember through another post of mine that you helped me with (https://stackoverflow.com/questions/47948454/messaginggateway-spring-cloud-stream-and-error-handling-across-both) that I've replaced my usage of RestTemplate to invoke remote APIs with `@MessagingGateway` after modifying all of my backend services to asynchronously process requests utilizing Spring Cloud Stream. I have frontend REST APIs that I'd still like to generate documentation for. – Keith Bennett Dec 29 '17 at 19:38
  • You seem to be conflating REST (API) Docs with REST controller internals - see if my answer helps. Since `@MessagingGatewau` is simply an interface, it's trivial to mock it. – Gary Russell Dec 29 '17 at 19:42

1 Answers1

1

If you mean you want to run REST Docs against a controller that has a mock interface injected into it, then something like this should work...

@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

}

Assumes

@MessagingGateway
public interface MyGateway {

    Bar foo(Foo foo);

}

However, mocking the gateway really has nothing to do with REST Docs.

If that's not what you mean, please expand your question.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • If you are using autowiring, simply omit the `@IntegrationComponentScan` from your test and instead add a mock `@Bean`. – Gary Russell Dec 29 '17 at 20:40