title: Spring MockRestServiceServer handling multiple requests to the same URI (auto-discovery)
body: <p>Let's say I am writing Spring integration tests for a REST service A. This service in turn hits another REST service B and gets a list of URIs to hit on REST service C. It is kind of auto-discovery pattern. I want to mock B and C responses using MockRestServiceServer.<br>
  Now the response from B is a list of URIs, they are all very similar, and for the sake of the example lets say my response from B is like so:</p>
  
  <pre><code>{
      uris: ["/stuff/1.json", "/stuff/2.json", "/stuff/39.json", "/stuff/47.json"]
  }
  </code></pre>
  
  <p>Simply service A will append each of them onto base URL for service C and make those requests.<br>
  Mocking B is easy since it is only 1 request.<br>
  Mocking C is a hassle as I would have to mock every single URI to appropriate mock response. I want to automate it!<br>
  So first I write my own matcher to match not a full URL, but part of it:  </p>
  
  <pre><code>public class RequestContainsUriMatcher implements RequestMatcher {
      private final String uri;
  
      public RequestContainsUriMatcher(String uri){
          this.uri = uri;
      }
  
      @Override
      public void match(ClientHttpRequest clientHttpRequest) throws IOException, AssertionError {
          assertTrue(clientHttpRequest.getURI().contains(uri));
      }
  }
  </code></pre>
  
  <p>This works fine as now I can do this:  </p>
  
  <pre><code>public RequestMatcher requestContainsUri(String uri) {
      return new RequestContainsUriMatcher(uri);
  }
  
  MockRestServiceServer.createServer(restTemplate)
              .expect(requestContainsUri("/stuff"))
              .andExpect(method(HttpMethod.GET))
              .andRespond(/* I will get to response creator */);
  </code></pre>
  
  <p>Now all I need is a response creator that knows the full request URL and where the mock data sits (I will have it as json files in test resources folder):</p>
  
  <pre><code>public class AutoDiscoveryCannedDataResponseCreator implements ResponseCreator {
      private final Function&lt;String, String&gt; cannedDataBuilder;
  
      public AutoDiscoveryCannedDataResponseCreator(Function&lt;String, String&gt; cannedDataBuilder) {
          this.cannedDataBuilder = cannedDataBuilder;
      }
  
      @Override
      public ClientHttpResponse createResponse(ClientHttpRequest clientHttpRequest) throws IOException {
          return withSuccess(cannedDataBuilder.apply(requestUri), MediaType.APPLICATION_JSON)
                      .createResponse(clientHttpRequest);
      }
  }
  </code></pre>
  
  <p>Now stuff is easy, I have to write a builder that takes request URI as a string and returns mock data, as a String! Brilliant!</p>
  
  <pre><code>public ResponseCreator withAutoDetectedCannedData() {
      Function&lt;String, String&gt; cannedDataBuilder = new Function&lt;String, String&gt;() {
          @Override
          public String apply(String requestUri) {
              //logic to get the canned data based on URI
              return cannedData;
          }
      };
  
      return new AutoDiscoveryCannedDataResponseCreator(cannedDataBuilder);
  }
  
  MockRestServiceServer.createServer(restTemplate)
              .expect(requestContainsUri("/stuff"))
              .andExpect(method(HttpMethod.GET))
              .andRespond(withAutoDetectedCannedData());
  </code></pre>
  
  <p>It works fine! .... For the first request.<br>
  After the first request (/stuff/1.json) my MockRestServiceServer responds with message "Assertion error: no further requests expected".<br>
  Basically, I can make as many requests to that MockRestServiceServer as there were .expect() calls on it. And since I had only 1 of them, only first request will go through.<br>
  Is there a way around it? I really don't want to mock service C 10 or 20 times...</p>