Skip to content

Commit

Permalink
Merge pull request quarkusio#18794 from geoand/quarkusio#18778
Browse files Browse the repository at this point in the history
Introduce failure assertions for UniAsserter
  • Loading branch information
gsmet authored Jul 19, 2021
2 parents f259726 + 9d987c2 commit 8719b71
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.persistence.PersistenceException;

import org.hibernate.reactive.mutiny.Mutiny.Transaction;
import org.junit.jupiter.api.Assertions;
Expand All @@ -21,6 +22,7 @@
import io.quarkus.test.TestReactiveTransaction;
import io.quarkus.test.junit.DisabledOnNativeImage;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.vertx.RunOnVertxContext;
import io.quarkus.test.junit.vertx.UniAsserter;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
Expand Down Expand Up @@ -260,4 +262,12 @@ public void testReactiveTransactional3(UniAsserter asserter) {
// and clean up
asserter.assertEquals(() -> Person.deleteAll(), 1l);
}

@DisabledOnNativeImage
@RunOnVertxContext
@Test
@Order(300)
public void testPersistenceException(UniAsserter asserter) {
asserter.assertFailedWith(() -> new Person().delete(), PersistenceException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,31 @@ public UniAsserter fail() {
execution = execution.onItem().invoke(v -> Assertions.fail());
return this;
}

@Override
public <T> UniAsserter assertFailedWith(Supplier<Uni<T>> uni, Consumer<Throwable> c) {
execution = uniFromSupplier(uni)
// return a new uni so we can avoid io.smallrye.mutiny.CompositeException
.onItemOrFailure().transformToUni((o, t) -> {
if (t == null) {
return Uni.createFrom().failure(() -> Assertions.fail("Uni did not contain a failure."));
} else {
return Uni.createFrom().item(() -> {
c.accept(t);
return null;
});
}
});
return this;
}

@Override
public <T> UniAsserter assertFailedWith(Supplier<Uni<T>> uni, Class<? extends Throwable> c) {
return assertFailedWith(uni, t -> {
if (!c.isInstance(t)) {
Assertions.fail(String.format("Uni failure type '%s' was not of the expected type '%s'.",
t.getClass().getName(), c.getName()));
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,19 @@ public interface UniAsserter {
UniAsserter assertTrue(Supplier<Uni<Boolean>> uni);

UniAsserter assertFalse(Supplier<Uni<Boolean>> uni);

/**
* Used to determine whether or not the Uni contains the expected failure.
* The assertions fails if the Uni does not fail.
*
* @param c Consumer that performs custom assertions on whether or not the failure matches expectations. This allows
* asserting on anything present in the {@code Throwable} like, the cause or the message
*/
<T> UniAsserter assertFailedWith(Supplier<Uni<T>> uni, Consumer<Throwable> c);

/**
* Used to determine whether or not the Uni contains the expected failure type.
* The assertions fails if the Uni does not fail.
*/
<T> UniAsserter assertFailedWith(Supplier<Uni<T>> uni, Class<? extends Throwable> c);
}

0 comments on commit 8719b71

Please sign in to comment.