oucem
oucem

Reputation: 79

@TestReactiveTransaction not rollbacked Quarkus Panache Hibernate reactive & mutiny

I add some unit tests to verify PanacheBaseRepository's method, and my problem is that rows inserted in data base not get rollbacked after test execution using quarkus 3.7.3.

I declared my repository as:

@ApplicationScoped
@WithTransaction
public class FieldRepository implements PanacheRepositoryBase<FieldEntity, UUID> {

    public Uni<FieldEntity> updatePreview(UUID id, JsonObject value) {
        return update("previewValue = ?1, updated = true where id= ?2", value, id)
                .replaceWith(findById(id));
    }
}

and my test:

@QuarkusTest
@TestReactiveTransaction
class AttributeRepositoryTest2 {

    @Test
    void shouldUpdateEntity(UniAsserter asserter) {
        FieldEntity entity = new FieldEntity();
        var updatedValue = "updatedValue";
        
        asserter.execute(() -> fieldRepository.persist(entity)) (1)
                .assertThat(() -> fieldRepository.updatePreview(entity.id, updatedValue), (2)
                        fieldEntity -> Assertions.assertThat(fieldEntity) (3)
                                .extracting(field -> field.previewValue, field -> field.updated)
                                .contains(updatedValue, true))
                .surroundWith(u -> Panache.withTransaction(() -> u));
    }
}

I tried chaining unis like

@QuarkusTest
@TestReactiveTransaction
class AttributeRepositoryTest2 {

    @Test
    void shouldUpdateEntity(UniAsserter asserter) {
        FieldEntity entity = new FieldEntity();
        var updatedValue = "updatedValue";
        
        asserter.assertThat(() -> fieldRepository.persist(entity).chain(field ->    fieldRepository.updatePreview(field.id, updatedValue)), (2)
                        fieldEntity -> Assertions.assertThat(fieldEntity) (3)
                                .extracting(field -> field.previewValue, field -> field.updated)
                                .contains(updatedValue, true))
                .surroundWith(u -> Panache.withTransaction(() -> u));
    }
}

same result, transaction not rollbacked and i have new record in DB.

Did i miss something, why does persist method not rollbacked?

Upvotes: 0

Views: 150

Answers (1)

Martin Kouba
Martin Kouba

Reputation: 1526

I think that you should remove the .surroundWith(u -> Panache.withTransaction(() -> u) part, because that's what the TestReactiveTransaction interceptor does, except that it calls Mutiny.Transaction.markForRollback().

Upvotes: 0

Related Questions