title: Spring MockMvc second test fails when using StreamingResponseBody
body: <p>I have a simple Spring Boot service that uses StreamingResponseBody to write the response.
  When testing this with MockMvc, the first test gives me the correct response, but the second one has an empty response body.
  However, when I add a little sleep time to the test, they both work.</p>
  <p>Here is my controller (Kotlin)</p>
  <pre><code>@RestController
  class DummyController() {
      @GetMapping()
      fun streamIndex() =
          ResponseEntity.ok()
              .contentType(MediaType.TEXT_PLAIN)
              .body(StreamingResponseBody { it.write(&quot;Hello world&quot;.toByteArray()) })
  }
  </code></pre>
  <p>And my tests:</p>
  <pre><code>@AutoConfigureMockMvc(print = MockMvcPrint.NONE)
  @TestInstance(TestInstance.Lifecycle.PER_CLASS)
  @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  class DummyControllerTest {
      private val controller = DummyController()
      private val mvc = MockMvcBuilders.standaloneSetup(controller).setControllerAdvice(GlobalExceptionHandler()).build()
  
      @Test
      fun test1() {
          mvc.perform(get(&quot;/&quot;)).andExpect(status().isOk).andExpect(content().string(&quot;Hello world&quot;))
      }
  
      @Test
      fun test2() {
          mvc.perform(get(&quot;/&quot;)).andExpect(status().isOk).andExpect(content().string(&quot;Hello world&quot;))
      }
  }
  </code></pre>
  <p>Thee first test succeeds, the second one fails with:</p>
  <pre><code>java.lang.AssertionError: Response content expected:&lt;Hello world&gt; but was:&lt;&gt;
  </code></pre>
  <p>Then when I add a little delay in the test it works:</p>
  <pre><code>@Test
  fun test2() {
      mvc.perform(get(&quot;/&quot;)).andExpect(status().isOk)
          .andDo { Thread.sleep(5) }
          .andExpect(content().string(&quot;Hello world&quot;))
  }
  </code></pre>
  <p>Is this a bug in MockMvc or something missing in my test?</p>
  <p>Thanks in advance,</p>
  <p>Rob</p>