title: MockRestServiceServer.verify() is failed when the test did not mock all of API in the project
body: <p>My project have 2 APIs and I want to separate test for each API.
  So I am trying to use MockRestServiceServer to mock my API but MockRestServiceServer requires all of API that I have in my project have to mock even it does not need.</p>
  <p>My spring boot version:</p>
  <pre><code>&lt;parent&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
      &lt;version&gt;2.6.6&lt;/version&gt;
      &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
  &lt;/parent&gt;
  </code></pre>
  <p>There are my code: using 2 schedules to call 2 APIs</p>
  <pre><code>@Scheduled(fixedDelay = 3000)
  public void schedule1() {
      restTemplate.getForEntity(URI.create(&quot;http://localhost:9090/api1&quot;), String.class);
  }
      
  @Scheduled(fixedDelay = 3000)
  public void schedule2() {
      restTemplate.getForEntity(URI.create(&quot;http://localhost:9090/api2&quot;), String.class);
  }
  </code></pre>
  <p>And my test</p>
  <pre><code>@Test
  void testSchedule1() {
      mockServer = MockRestServiceServer.bindTo(restTemplate).ignoreExpectOrder(true).build();
      mockServer
          .expect(ExpectedCount.manyTimes(),
              MockRestRequestMatchers.requestTo(new StringStartsWith(&quot;http://localhost:9090/api1&quot;)))
          .andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
          .andRespond(MockRestResponseCreators.withStatus(HttpStatus.OK).body(&quot;&quot;));
  
      mockServer.verify(Duration.ofSeconds(10));
  }
      
  @Test
  void testSchedule2() {
      mockServer = MockRestServiceServer.bindTo(restTemplate).ignoreExpectOrder(true).build();
      mockServer
          .expect(ExpectedCount.manyTimes(),
              MockRestRequestMatchers.requestTo(new StringStartsWith(&quot;http://localhost:9090/api2&quot;)))
          .andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
          .andRespond(MockRestResponseCreators.withStatus(HttpStatus.OK).body(&quot;&quot;));
  
      mockServer.verify(Duration.ofSeconds(10));
  }
  </code></pre>
  <p>As default, 2 schedules will run in the same thread. So schedule1 executed first, then the testSchedule1 passed and testSchedule2 always failed.</p>
  <pre><code>2022-04-02 21:50:30.374 ERROR 2284 --- \[   scheduling-1\] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task
  
  org.springframework.web.client.ResourceAccessException: I/O error on GET request for &quot;http://localhost:9090/api1&quot;: Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
  at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:785) \~\[spring-web-5.3.18.jar:5.3.18\]
  at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:751) \~\[spring-web-5.3.18.jar:5.3.18\]
  at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:377) \~\[spring-web-5.3.18.jar:5.3.18\]
  at com.example.demo1.test.Test.schedule1(Test.java:18) \~\[classes/:na\]
  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) \~\[na:na\]
  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) \~\[na:na\]
  at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) \~\[na:na\]
  at java.base/java.lang.reflect.Method.invoke(Method.java:568) \~\[na:na\]
  at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) \~\[spring-context-5.3.18.jar:5.3.18\]
  at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) \~\[spring-context-5.3.18.jar:5.3.18\]
  at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) \~\[na:na\]
  at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305) \~\[na:na\]
  at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305) \~\[na:na\]
  at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) \~\[na:na\]
  at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) \~\[na:na\]
  at java.base/java.lang.Thread.run(Thread.java:833) \~\[na:na\]
  Caused by: java.net.ConnectException: Connection refused: connect
  at java.base/sun.nio.ch.Net.connect0(Native Method) \~\[na:na\]
  at java.base/sun.nio.ch.Net.connect(Net.java:579) \~\[na:na\]
  at java.base/sun.nio.ch.Net.connect(Net.java:568) \~\[na:na\]
  at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588) \~\[na:na\]
  at java.base/java.net.Socket.connect(Socket.java:633) \~\[na:na\]
  at java.base/java.net.Socket.connect(Socket.java:583) \~\[na:na\]
  at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:183) \~\[na:na\]
  at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:498) \~\[na:na\]
  at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:603) \~\[na:na\]
  at java.base/sun.net.www.http.HttpClient.\&lt;init\&gt;(HttpClient.java:246) \~\[na:na\]
  at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:351) \~\[na:na\]
  at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:373) \~\[na:na\]
  at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1309) \~\[na:na\]
  at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1242) \~\[na:na\]
  at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1128) \~\[na:na\]
  at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1057) \~\[na:na\]
  at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76) \~\[spring-web-5.3.18.jar:5.3.18\]
  at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) \~\[spring-web-5.3.18.jar:5.3.18\]
  at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:66) \~\[spring-web-5.3.18.jar:5.3.18\]
  at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:776) \~\[spring-web-5.3.18.jar:5.3.18\]
  ... 15 common frames omitted
  
  2022-04-02 21:50:30.376 ERROR 2284 --- \[   scheduling-1\] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task
  
  org.springframework.web.client.ResourceAccessException: I/O error on GET request for &quot;http://localhost:9090/api2&quot;: Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
  at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:785) \~\[spring-web-5.3.18.jar:5.3.18\]
  at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:751) \~\[spring-web-5.3.18.jar:5.3.18\]
  at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:377) \~\[spring-web-5.3.18.jar:5.3.18\]
  at com.example.demo1.test.Test.schedule2(Test.java:23) \~\[classes/:na\]
  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) \~\[na:na\]
  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) \~\[na:na\]
  at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) \~\[na:na\]
  at java.base/java.lang.reflect.Method.invoke(Method.java:568) \~\[na:na\]
  at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) \~\[spring-context-5.3.18.jar:5.3.18\]
  at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) \~\[spring-context-5.3.18.jar:5.3.18\]
  at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) \~\[na:na\]
  at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305) \~\[na:na\]
  at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305) \~\[na:na\]
  at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) \~\[na:na\]
  at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) \~\[na:na\]
  at java.base/java.lang.Thread.run(Thread.java:833) \~\[na:na\]
  Caused by: java.net.ConnectException: Connection refused: connect
  at java.base/sun.nio.ch.Net.connect0(Native Method) \~\[na:na\]
  at java.base/sun.nio.ch.Net.connect(Net.java:579) \~\[na:na\]
  at java.base/sun.nio.ch.Net.connect(Net.java:568) \~\[na:na\]
  at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588) \~\[na:na\]
  at java.base/java.net.Socket.connect(Socket.java:633) \~\[na:na\]
  at java.base/java.net.Socket.connect(Socket.java:583) \~\[na:na\]
  at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:183) \~\[na:na\]
  at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:498) \~\[na:na\]
  at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:603) \~\[na:na\]
  at java.base/sun.net.www.http.HttpClient.\&lt;init\&gt;(HttpClient.java:246) \~\[na:na\]
  at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:351) \~\[na:na\]
  at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:373) \~\[na:na\]
  at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1309) \~\[na:na\]
  at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1242) \~\[na:na\]
  at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1128) \~\[na:na\]
  at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1057) \~\[na:na\]
  at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76) \~\[spring-web-5.3.18.jar:5.3.18\]
  at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) \~\[spring-web-5.3.18.jar:5.3.18\]
  at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:66) \~\[spring-web-5.3.18.jar:5.3.18\]
  at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:776) \~\[spring-web-5.3.18.jar:5.3.18\]
  ... 15 common frames omitted
  
  2022-04-02 21:50:33.392 ERROR 2284 --- \[   scheduling-1\] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task
  
  java.lang.AssertionError: No further requests expected: HTTP GET http://localhost:9090/api1
  0 request(s) executed.
  
      at org.springframework.test.web.client.AbstractRequestExpectationManager.createUnexpectedRequestError(AbstractRequestExpectationManager.java:213) ~[spring-test-5.3.18.jar:5.3.18]
      at org.springframework.test.web.client.UnorderedRequestExpectationManager.matchRequest(UnorderedRequestExpectationManager.java:44) ~[spring-test-5.3.18.jar:5.3.18]
      at org.springframework.test.web.client.AbstractRequestExpectationManager.validateRequest(AbstractRequestExpectationManager.java:97) ~[spring-test-5.3.18.jar:5.3.18]
      at org.springframework.test.web.client.MockRestServiceServer$MockClientHttpRequestFactory$1.executeInternal(MockRestServiceServer.java:338) ~[spring-test-5.3.18.jar:5.3.18]
      at org.springframework.mock.http.client.MockClientHttpRequest.execute(MockClientHttpRequest.java:110) ~[spring-test-5.3.18.jar:5.3.18]
      at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:776) ~[spring-web-5.3.18.jar:5.3.18]
      at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:751) ~[spring-web-5.3.18.jar:5.3.18]
      at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:377) ~[spring-web-5.3.18.jar:5.3.18]
      at com.example.demo1.test.Test.schedule1(Test.java:18) ~[classes/:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
      at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
      at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.3.18.jar:5.3.18]
      at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.3.18.jar:5.3.18]
      at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) ~[na:na]
      at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305) ~[na:na]
      at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305) ~[na:na]
      at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[na:na]
      at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[na:na]
      at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
  </code></pre>
  <p>After checked, I recognized If any schedule run first, the test case of that schedule will be passed.
  I have checked the source code of MockRestServiceServer. It comes to AbstractRequestExpectationManager
  in the verify method will call verifyInternal()</p>
  <pre><code>private int verifyInternal() {
      if (this.expectations.isEmpty()) {
          return 0;
      }
      if (!this.requestFailures.isEmpty()) {
          throw new AssertionError(&quot;Some requests did not execute successfully.\n&quot; +
                  this.requestFailures.entrySet().stream()
                      .map(entry -&gt; &quot;Failed request:\n&quot; + entry.getKey() + &quot;\n&quot; + entry.getValue())
                      .collect(Collectors.joining(&quot;\n&quot;, &quot;\n&quot;, &quot;&quot;)));
       }
       int count = 0;
       for (RequestExpectation expectation : this.expectations) {
           if (!expectation.isSatisfied()) {
               count++;
           }
       }
       return count;
  }
  </code></pre>
  <p>And the <strong>requestFailures</strong> is the map stores all failed requests (expected and unexpected)</p>
  <pre><code>  @Override
    public ClientHttpResponse validateRequest(ClientHttpRequest request) throws IOException {
      RequestExpectation expectation;
      synchronized (this.requests) {
        if (this.requests.isEmpty()) {
          afterExpectationsDeclared();
        }
        try {
          // Try this first for backwards compatibility
          ClientHttpResponse response = validateRequestInternal(request);
          if (response != null) {
            return response;
          }
          else {
            expectation = matchRequest(request);
          }
        }
        catch (Throwable ex) {
          this.requestFailures.put(request, ex);
          throw ex;
        }
        finally {
          this.requests.add(request);
        }
      }
      return expectation.createResponse(request);
    }
  </code></pre>
  <p>If the schedule1 will be executed first, then it satisfied the condition: <strong>if (this.expectations.isEmpty())</strong> first and the test case will be passed.
  If the schedule1 executed after schedule1, the <strong>requestFailures</strong> will store the error of API 1, then it satisfied this condition: <strong>if (!this.requestFailures.isEmpty())</strong> and the test case will be failed.</p>
  <p>Is that MockRestServiceServer behavior? They want to mock all APIs?
  If yes, please help me some alternative solution for my scenario.</p>
  <p>Thanks</p>