/// <summary> /// Asserts that the lambda-based code throws an exception /// and that the cause of the exception is the supplied cause. /// <para> /// For example: /// <pre> /// assertThrowsWithCause(() -> /// executeSql("INSERT DATA THAT ALREADY EXISTS"), SQLIntegrityConstraintViolationException.class); /// </pre> /// /// </para> /// </summary> /// <param name="runner"> the lambda containing the code to test </param> /// <param name="cause"> the expected cause of the exception thrown </param> public static void assertThrowsWithCause(AssertRunnable runner, Type cause) { assertNotNull(runner, "assertThrowsWithCause() called with null AssertRunnable"); assertNotNull(cause, "assertThrowsWithCause() called with null expected cause"); try { runner(); fail("Expected " + cause.Name + " but code succeeded normally"); } catch (AssertionError ex) { throw ex; } catch (Exception ex) { Exception ex2 = ex; while (ex2 != null && !cause.IsInstanceOfType(ex2)) { ex2 = ex2.InnerException; } if (ex2 == null) { fail("Expected cause of exception to be: " + cause.Name + " but got different exception", ex); } } }
private static void assertThrowsImpl(AssertRunnable runner, Type expected, string regex) { assertNotNull(runner, "assertThrows() called with null AssertRunnable"); assertNotNull(expected, "assertThrows() called with null expected Class"); try { runner(); fail("Expected " + expected.Name + " but code succeeded normally"); } catch (AssertionError ex) { throw ex; } catch (Exception ex) { if (expected.IsInstanceOfType(ex)) { string message = ex.Message; if (string.ReferenceEquals(regex, null) || message.matches(regex)) { return; } else { fail("Expected exception message to match: [" + regex + "] but received: " + message); } } fail("Expected " + expected.Name + " but received " + ex.GetType().Name, ex); } }
/// <summary> /// Ignore any exception thrown by the lambda-based code. /// <para> /// For example: /// <pre> /// ignoreThrows(() -> bean.property("")); /// </pre> /// /// </para> /// </summary> /// <param name="runner"> the lambda containing the code to test </param> public static void ignoreThrows(AssertRunnable runner) { assertNotNull(runner, "ignoreThrows() called with null AssertRunnable"); try { runner(); } catch (Exception) { // ignore } }
/// <summary> /// Asserts that the lambda-based code throws an {@code IllegalArgumentException} and checks the message /// matches an regex. /// <para> /// For example: /// <pre> /// assertThrowsIllegalArg(() -> new Foo(null), "Foo constructor argument must not be null"); /// </pre> /// /// </para> /// </summary> /// <param name="runner"> the lambda containing the code to test </param> /// <param name="regex"> regular expression that must match the exception message </param> public static void assertThrowsIllegalArg(AssertRunnable runner, string regex) { assertThrows(runner, typeof(System.ArgumentException), regex); }
/// <summary> /// Asserts that the lambda-based code throws an {@code RuntimeException}. /// <para> /// For example: /// <pre> /// assertThrowsRuntime(() -> new Foo(null)); /// </pre> /// /// </para> /// </summary> /// <param name="runner"> the lambda containing the code to test </param> public static void assertThrowsRuntime(AssertRunnable runner) { assertThrows(runner, typeof(Exception)); }
/// <summary> /// Asserts that the lambda-based code throws the specified exception /// and that the exception message matches the supplied regular /// expression. /// <para> /// For example: /// <pre> /// assertThrows(() -> bean.property(""), NoSuchElementException.class, "Unknown property.*"); /// </pre> /// /// </para> /// </summary> /// <param name="runner"> the lambda containing the code to test </param> /// <param name="expected"> the expected exception </param> /// <param name="regex"> the regex that the exception message is expected to match </param> public static void assertThrows(AssertRunnable runner, Type expected, string regex) { assertNotNull(regex, "assertThrows() called with null regex"); assertThrowsImpl(runner, expected, regex); }
//------------------------------------------------------------------------- /// <summary> /// Asserts that the lambda-based code throws the specified exception. /// <para> /// For example: /// <pre> /// assertThrows(() -> bean.property(""), NoSuchElementException.class); /// </pre> /// /// </para> /// </summary> /// <param name="runner"> the lambda containing the code to test </param> /// <param name="expected"> the expected exception </param> public static void assertThrows(AssertRunnable runner, Type expected) { assertThrowsImpl(runner, expected, null); }