How can I reset database each test in spring project?

Viewed 37

I've tried to use @DirtiesContext but it doesn't work. I want to make every class with integration tests isolated.

1 Answers

You can use @DataJpaTest because @DataJpaTest are transactional and roll back at the end of each test or just write clean script if you need full context by using @SpringBootTest

@SpringBootTest
@Sql(scripts = "classpath:clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD,
        config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED))
Related