From 53795e781977d92ef8c1fa0d93a35d3ab3b0ee5b Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Fri, 2 Dec 2022 08:03:02 +0100 Subject: [PATCH] UniAsserter - ignore the assertFailedWith() if a previous stage fails - resolves #29614 --- .../test/vertx/DefaultUniAsserter.java | 23 +++++++++------- .../quarkus/test/vertx/UniAsserterTest.java | 26 +++++++++++++++---- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/test-framework/vertx/src/main/java/io/quarkus/test/vertx/DefaultUniAsserter.java b/test-framework/vertx/src/main/java/io/quarkus/test/vertx/DefaultUniAsserter.java index 1820d442918cd..94d22af37c58a 100644 --- a/test-framework/vertx/src/main/java/io/quarkus/test/vertx/DefaultUniAsserter.java +++ b/test-framework/vertx/src/main/java/io/quarkus/test/vertx/DefaultUniAsserter.java @@ -148,17 +148,22 @@ public UniAsserter fail() { return this; } + @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public UniAsserter assertFailedWith(Supplier> uni, Consumer 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>() { + @Override + public Uni 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; + }); + } }); } }); diff --git a/test-framework/vertx/src/test/java/io/quarkus/test/vertx/UniAsserterTest.java b/test-framework/vertx/src/test/java/io/quarkus/test/vertx/UniAsserterTest.java index 24be00e8bf0d8..2ddd35b4e4a44 100644 --- a/test-framework/vertx/src/test/java/io/quarkus/test/vertx/UniAsserterTest.java +++ b/test-framework/vertx/src/test/java/io/quarkus/test/vertx/UniAsserterTest.java @@ -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; @@ -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 @@ -180,6 +190,10 @@ private void testAsserter(Consumer assertion, T finalItem) { } private void testAsserterFailure(Consumer assertion) { + testAsserterFailure(assertion, null); + } + + private void testAsserterFailure(Consumer assertion, Predicate expected) { CompletableFuture cf = new CompletableFuture<>(); DefaultUniAsserter asserter = new DefaultUniAsserter(); assertion.accept(asserter); @@ -188,7 +202,9 @@ private void testAsserterFailure(Consumer 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); }