Use the correct information when throwing Exceptions for failed tests. ()

* Use the correct information when throwing Exceptions for failed tests.

* Re-throw the exception if it is already a RuntimeException

* Use pattern matching instead of casting
This commit is contained in:
Jared 2022-01-02 20:56:08 +02:00 committed by GitHub
parent 9481101883
commit c5d03bcd0e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -102,8 +102,14 @@ public final class FabricGameTestHelper {
public static void invokeTestMethod(TestContext testContext, Method method, Object testObject) {
try {
method.invoke(testObject, testContext);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("Failed to invoke test method (%s) in (%s)".formatted(method.getName(), method.getDeclaringClass().getCanonicalName()), e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Failed to invoke test method (%s) in (%s) because %s".formatted(method.getName(), method.getDeclaringClass().getCanonicalName(), e.getMessage()), e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof RuntimeException runtimeException) {
throw runtimeException;
} else {
throw new RuntimeException(e.getCause());
}
}
}