From 2a4cf1a54d3dbe91739aa3c3460bef2a1260c636 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 17 Apr 2025 23:02:57 +0200 Subject: [PATCH] plugin-tests: Add toThrow method for exception assertion in Expectable --- .../main/java/de/oliver/plugintests/Expectable.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libraries/plugin-tests/src/main/java/de/oliver/plugintests/Expectable.java b/libraries/plugin-tests/src/main/java/de/oliver/plugintests/Expectable.java index 56d1a7ee..352ce9e3 100644 --- a/libraries/plugin-tests/src/main/java/de/oliver/plugintests/Expectable.java +++ b/libraries/plugin-tests/src/main/java/de/oliver/plugintests/Expectable.java @@ -240,4 +240,16 @@ public class Expectable { throw new AssertionError("toHaveLength can only be used on Strings"); } + + public E toThrow(Class 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"); + } }