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?
@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