Persisting object in a test does not save it to the DB

Viewed 30

I have an app running on a Quarkus server. I have a Postgres DB using hibernate and implement PanacheRepositoryBase. When the app runs, things runs smoothly but when I try to run integration tests, the db doesn't get populated.

I have a beforeEach call and here I add a record in the Company table, but then in the test I try to add a user and that table is empty. The thing that confuses me is when I persist the object I return findById() and it returns an object, but yet the table is empty. I am really confused by this...

@ApplicationScoped
public class UserRepository implements PanacheRepositoryBase<UserEntity, Long> {

    @Transactional
    public UserEntity persistUser(UserEntity user) {
        persist(user);
        return findById(user.getId());
    }
}
@QuarkusTransactionalTest
@QuarkusTestResource(PostgresResource.class)
public class UserRepositoryIntegrationTest {

    @Inject
    UserRepository repository;

    @Inject
    CompanyRepository companyRepo;

    private CompanyEntity company;

    @BeforeEach
    void setup() {
        company = companyRepo.persistCompany(createCompany()); // Added to DB
    }

    @AfterEach
    void cleanup() {
        repository.deleteAll();
        companyRepo.deleteAll();
    }

    @Test
    void test_persistUser() {
        // UserEntity is returned
        UserEntity user = repository.persistUser(createUser(company, true, true)); 
        assertNotNull(user);

        UserEntity user2 = reposiitory.findById(user.getId());
        assertNotNull(user2); // This fails as it is null
    }
}
0 Answers
Related