Skip to content

Commit

Permalink
Merge pull request quarkusio#29633 from mkouba/issue-29614
Browse files Browse the repository at this point in the history
UniAsserter - ignore the assertFailedWith() if a previous stage fails
  • Loading branch information
geoand authored Dec 2, 2022
2 parents eaaeba4 + 53795e7 commit abee5fb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,22 @@ public UniAsserter fail() {
return this;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@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;
execution = execution.onItem()
.transformToUni((Function) new Function<Object, Uni<T>>() {
@Override
public Uni<T> apply(Object obj) {
return uni.get().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;
});
}
});
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.quarkus.test.vertx;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Predicate;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -56,12 +58,20 @@ public void testAssertFailedWith() {
testAsserter(ua -> ua.assertFailedWith(() -> Uni.createFrom().failure(new NullPointerException()),
NullPointerException.class));
testAsserterFailure(ua -> ua.assertFailedWith(() -> Uni.createFrom().failure(new IllegalStateException()),
NullPointerException.class));
NullPointerException.class), t -> AssertionError.class.isInstance(t));

// Note that assertFailedWith() is not tested at all because of the exception thrown from the previous assertEquals()
testAsserterFailure(ua -> ua.assertEquals(() -> Uni.createFrom().item("foo"), null)
.assertFailedWith(() -> Uni.createFrom().failure(new NullPointerException()),
IllegalArgumentException.class),
t -> AssertionError.class.isInstance(t));

// TODO note that assertFailedWith() receives the exception thrown from the previous assertEquals()
testAsserter(ua -> ua.assertEquals(() -> Uni.createFrom().item("foo"), null)
testAsserterFailure(ua -> ua.assertTrue(() -> {
throw new IllegalArgumentException();
})
.assertFailedWith(() -> Uni.createFrom().failure(new NullPointerException()),
AssertionError.class));
IllegalArgumentException.class),
t -> IllegalArgumentException.class.isInstance(t));
}

@Test
Expand Down Expand Up @@ -180,6 +190,10 @@ private <T> void testAsserter(Consumer<UniAsserter> assertion, T finalItem) {
}

private void testAsserterFailure(Consumer<UniAsserter> assertion) {
testAsserterFailure(assertion, null);
}

private void testAsserterFailure(Consumer<UniAsserter> assertion, Predicate<Throwable> expected) {
CompletableFuture<Object> cf = new CompletableFuture<>();
DefaultUniAsserter asserter = new DefaultUniAsserter();
assertion.accept(asserter);
Expand All @@ -188,7 +202,9 @@ private void testAsserterFailure(Consumer<UniAsserter> assertion) {
cf.get();
fail("No failure");
} catch (ExecutionException e) {
// expected
if (expected != null) {
assertTrue(expected.test(e.getCause()));
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit abee5fb

Please sign in to comment.