jeudi 14 décembre 2017

Is it possible to test Spring REST Controllers without @DirtiesContext?

I'm working on a Spring-Boot web application. The usual way of writing integration tests is:

@Test
@Transactional
@Rollback(true)
public void myTest() {
    // ...
}

This works nicely as long as only one thread does the work. @Rollback cannot work if there are multiple threads.

However, when testing a @RestController class using the Spring REST templates, there are always multiple threads (by design):

  • the test thread which acts as the client and runs the REST template
  • the server thread which receives and processes the request

So you can't use @Rollback in a REST test. The question is: what do you use instead to make tests repeatable and have them play nicely in a test suite?

@DirtiesContext 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.

Aucun commentaire:

Enregistrer un commentaire