plugin-tests: Add toThrow method for exception assertion in Expectable

This commit is contained in:
Oliver
2025-04-17 23:02:57 +02:00
parent af2e40e00b
commit 2a4cf1a54d

View File

@@ -240,4 +240,16 @@ public class Expectable<T> {
throw new AssertionError("toHaveLength can only be used on Strings");
}
public <E extends Throwable> E toThrow(Class<E> expected) {
try {
((Runnable) t).run();
} catch (Throwable e) {
if (expected.isInstance(e)) {
return (E) e;
}
throw new AssertionError("Expected " + expected.getSimpleName() + " but got " + e.getClass().getSimpleName());
}
throw new AssertionError("Expected " + expected.getSimpleName() + " but got nothing");
}
}