Skip to content
Permalink
Browse files
support assert-throws return future (#102)
Change-Id: I8fe115a7518dd84e22c9e28f777515e74e48d5d2
  • Loading branch information
javeme committed May 27, 2022
1 parent db84121 commit 589ee2ca10d97f36f3f3ccd1e00b538268489a55
Showing 1 changed file with 20 additions and 7 deletions.
@@ -19,6 +19,7 @@

package com.baidu.hugegraph.testutil;

import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;

@@ -40,31 +41,43 @@ public interface ThrowableRunnable {

public static void assertThrows(Class<? extends Throwable> throwable,
ThrowableRunnable runnable) {
assertThrows(throwable, runnable, e -> {
CompletableFuture<?> future = assertThrowsFuture(throwable, runnable);
future.thenAccept(e -> {
System.err.println(e);
});
}

public static void assertThrows(Class<? extends Throwable> throwable,
ThrowableRunnable runnable,
Consumer<Throwable> exceptionConsumer) {
CompletableFuture<Throwable> future = assertThrowsFuture(throwable,
runnable);
future.thenAccept(exceptionConsumer);
}

public static CompletableFuture<Throwable> assertThrowsFuture(
Class<? extends Throwable> clazz,
ThrowableRunnable runnable) {
CompletableFuture<Throwable> future = new CompletableFuture<>();
boolean fail = false;
try {
runnable.run();
fail = true;
} catch (Throwable e) {
if (!throwable.isInstance(e)) {
if (!clazz.isInstance(e)) {
Assert.fail(String.format(
"Bad exception type %s(expected %s)",
e.getClass().getName(), throwable.getName()));
e.getClass().getName(), clazz.getName()));
}
exceptionConsumer.accept(e);
future.complete(e);
}
if (fail) {
Assert.fail(String.format(
"No exception was thrown(expected %s)",
throwable.getName()));
String msg = String.format("No exception was thrown(expected %s)",
clazz.getName());
future.completeExceptionally(new AssertionError(msg));
Assert.fail(msg);
}
return future;
}

public static void assertEquals(byte expected, Object actual) {

0 comments on commit 589ee2c

Please sign in to comment.