mercredi 2 septembre 2020

Rollback Transactions when testing with Spring MockMvc

I have seen multiple threads about it. However, no one really solves my problem.

I have a Spring Boot 2.3 application with the three traditional layers: Controller, Service and DAO. The transactions are declared in my Service layer.

I would like to test my Controller layer using MockMvc, and I want the transactions to rollback at the end of the tests so that they all remain independent. However, I don't want the tests to give the Controller classes an access to the transactional context in order to have the same configuration as in runtime.

I created the following class:

@SpringBootTest
@AutoConfigureMockMvc
public class ApiIT {

    @Autowired
    private MockMvc mvc;

    @Test
    void restEndpointTest() {
...

This configuration doesn't rollback the transactions at the end of the tests. When I annotate the class with @Transactional, they rollback, but the Controller classes can access the transactional context.

Aucun commentaire:

Enregistrer un commentaire