title: Is it possible to test Spring REST Controllers without @DirtiesContext?
body: <p>I'm working on a Spring-Boot web application. The usual way of writing integration tests is:</p>
  
  <pre><code>@Test
  @Transactional
  @Rollback(true)
  public void myTest() {
      // ...
  }
  </code></pre>
  
  <p>This works nicely as long as only <em>one thread</em> does the work. <code>@Rollback</code> cannot work if there are multiple threads.</p>
  
  <p>However, when testing a <code>@RestController</code> class using the Spring REST templates, there are <em>always</em> multiple threads (by design):</p>
  
  <ul>
  <li>the test thread which acts as the client and runs the REST template</li>
  <li>the server thread which receives and processes the request</li>
  </ul>
  
  <p>So you can't use <code>@Rollback</code> in a REST test.  The question is: <strong>what do you use instead to make tests repeatable and have them play nicely in a test suite?</strong> </p>
  
  <p><code>@DirtiesContext</code> works, but is a bad option because restarting the Spring application context after each REST test method makes the suite really slow to execute; each test takes a couple of milliseconds to run, but restarting the context takes several seconds.</p>