How to reset / drop H2 database in Quarkus after each test method to make them independed?
Asked Answered
J

3

5

I´m using the H2 database in my Quarkus project with the @QuarkusTestResource annotation. Each test method is doing tests and checks if a certain number of users exist etc.

The problem I´m facing is that the database won´t be resetted after each test run, which is why the test fail as they are getting results of previous test runs.

@QuarkusTestResource(value = H2DatabaseTestResource.class)
class UserServiceTest {


    @Inject
    UserService userService;

    @Inject
    UserRepository userRepository;

    private User userA;
    private User userB;

    @Transactional
    @BeforeEach
    void setUp() {
        userA = new User();
        userA.setEmail("a");
        userA.setName("a");

        userB = new User();
        userB.setName("b");
        userB.setEmail("b");

        userRepository.persist(userA);
        userRepository.persist(userB);
    }

    @Test
    void testA(){
       //count == 2
    }


    @Test
    void testA(){
       //count == 4
    }


}

How do I reset the H2 database after each test to make them independend from each other?

Jovanjove answered 13/11, 2020 at 22:14 Comment(0)
K
3

I'm using @TestTransaction instead of @Transaction in each of my tests.

@QuarkusTest
public class TestInvoiceRepositoryfindAllInvoicesToBePaid {

  @Inject
  MyRepository myRepository;

  @Test
  @TestTransaction
  public void do_something_in_database_01() {
    // it generates and persist data I need for my test
    generateTestData();
    // test my repo method to make sure it does what I want
    MyObject myObject = myRepository.findSomething();
    assertNotNull(myObject);
  }

  @Test
  @TestTransaction
  public void do_something_in_database_02() {
    // here I need to insert the data again, because
    // the previous test rolled everything back at the end of it
    generateTestData();
    // test my repo method to make sure it does what I want
    myRepository.findSomething2();
  }

  private void generateTestData() {
    ...
    ...
    ...
    myRepository.persist(something);
  }
}
Kura answered 24/1, 2022 at 5:3 Comment(1)
I was using @Transactional on my tests, which prevented the database to shutdown between tests, replacing this annotation with @TestTransaction solved it for me. I've also added @QuarkusTestResource(H2DatabaseTestResource.class)Jenicejeniece
D
3

I suppose TestContainers may solve your problem.

There is a specific chapter about databases and theirs initialization can be done in a different way.

You can also use an in-memory database to avoid the use of a physical database.

Deettadeeyn answered 15/11, 2020 at 5:50 Comment(0)
R
3

Check the following JUnit Extension (with Flyway), which does what you want: https://github.com/radcortez/flyway-junit5-extensions

It also has a sample with Quarkus: https://github.com/radcortez/flyway-junit5-extensions/tree/master/examples/quarkus

Note: I was experiencing the same issue, so I wrote that extension to solve the use case. Please, let me know if that fixes yours. If not, I'm happy to enhance the extension.

Regeniaregensburg answered 18/11, 2020 at 10:37 Comment(0)
K
3

I'm using @TestTransaction instead of @Transaction in each of my tests.

@QuarkusTest
public class TestInvoiceRepositoryfindAllInvoicesToBePaid {

  @Inject
  MyRepository myRepository;

  @Test
  @TestTransaction
  public void do_something_in_database_01() {
    // it generates and persist data I need for my test
    generateTestData();
    // test my repo method to make sure it does what I want
    MyObject myObject = myRepository.findSomething();
    assertNotNull(myObject);
  }

  @Test
  @TestTransaction
  public void do_something_in_database_02() {
    // here I need to insert the data again, because
    // the previous test rolled everything back at the end of it
    generateTestData();
    // test my repo method to make sure it does what I want
    myRepository.findSomething2();
  }

  private void generateTestData() {
    ...
    ...
    ...
    myRepository.persist(something);
  }
}
Kura answered 24/1, 2022 at 5:3 Comment(1)
I was using @Transactional on my tests, which prevented the database to shutdown between tests, replacing this annotation with @TestTransaction solved it for me. I've also added @QuarkusTestResource(H2DatabaseTestResource.class)Jenicejeniece

© 2022 - 2024 — McMap. All rights reserved.