Search code examples
javahibernatejunitspring-data-jpahsqldb

isolate Junit tests using hsql hibernate datasource


I'm writing Junit tests for my Spring app. When I run each test individually it works, but when I run them as a whole the data from each test is persisting. The tests are using hsql tables generated by the hibernate entities. Is there any way to isolate the tests so that each test is dealing with empty tables?

thanks!


Solution

  • You can mark your test methods or test class as @Transactional:

    Annotating a test method with @Transactional causes the test to be run within a transaction that will, by default, be automatically rolled back after completion of the test. If a test class is annotated with @Transactional, each test method within that class hierarchy will be run within a transaction.

    More info: 1, 2