TransactionalUniAsserter tests Hibernate Reactive persistence without treating asynchronous Uni operations like ordinary synchronous calls. Inject it into a test running on the Vert.x event-loop thread; each asserted operation executes in a new transaction. section 7.7.2
Use the asserter to coordinate reactive persistence checks:
- Call assertThat with a Uni-producing operation and verify its result with assertions. section 7.7.2
- Use assertEquals for follow-up asynchronous queries such as counting persisted entities. section 7.7.2
- Pass values between operations with putData and getData, such as a generated reservation identifier. section 7.7.2
Reservation persistence test
A test creates a reservation with asserter.assertThat(() -> reservation.persist(), ...), verifies its generated identifier, stores that identifier with putData, counts persisted reservations, and retrieves the entity with findById(getData(...)). section 7.7.2
Listing 7.43 Reactive invocations in the ReservationPersistenceTest
@Test
@RunOnVertxContext
public void testCreateReservation(TransactionalUniAsserter asserter) {
Reservation reservation = new Reservation();
reservation.startDay = LocalDate.now().plus(5, ChronoUnit.DAYS);
reservation.endDay = LocalDate.now().plus(12, ChronoUnit.DAYS);
reservation.carId = 384L;
asserter.<Reservation>assertThat(() -> reservation.persist(),
r -> {
Assertions.assertNotNull(r.id);
asserter.putData("reservation.id", r.id);
});
asserter.assertEquals(() -> Reservation.count(), 1L);
asserter.assertThat(() -> Reservation.<Reservation>findById(
asserter.getData("reservation.id")),
persistedReservation -> {
Assertions.assertNotNull(persistedReservation);
Assertions.assertEquals(reservation.carId,
persistedReservation.carId);
});
}
Inspect the complete reactive invocation pattern for creating, counting, and retrieving a reservation. section 7.7.2
