public static Action throwException(Throwable throwable) { return new ThrowAction(throwable); }
public Object invoke(Invocation invocation) throws Throwable { if (isThrowingCheckedException()) { checkTypeCompatiblity(invocation.getInvokedMethod().getExceptionTypes()); } throwable.fillInStackTrace(); throw throwable; }
private void checkTypeCompatiblity(Class<?>[] allowedExceptionTypes) { for (int i = 0; i < allowedExceptionTypes.length; i++) { if (allowedExceptionTypes[i].isInstance(throwable)) return; } reportIncompatibleCheckedException(allowedExceptionTypes); }
public void testDoesNotCheckTypeCompatiblityOfUncheckedExceptions() throws Throwable { throwAction = new ThrowAction(new RuntimeException()); try { throwAction.invoke(invocation); } catch (RuntimeException ex) { return; } fail("should have thrown a RuntimeException"); }
public void testThrowsThrowableObjectPassedToConstructorWhenInvoked() { try { throwAction.invoke(invocation); } catch (Throwable t) { assertSame("Should be the same throwable", THROWABLE, t); } }
public void testDoesNotCheckTypeCompatiblityOfErrors() throws Throwable { throwAction = new ThrowAction(new Error()); try { throwAction.invoke(invocation); } catch (AssertionFailedError err) { throw err; } catch (Error ex) { return; } fail("should have thrown an Error"); }
public void testSetsStackTraceWhenExceptionIsThrown() { try { throwAction.invoke(invocation); } catch (Throwable t) { StackTraceElement[] stackTrace = t.getStackTrace(); assertEquals("thrown from ThrowAction object", throwAction.getClass().getName(), stackTrace[0].getClassName()); } } }
@Override public void setUp() { methodFactory = new MethodFactory(); invocation = new Invocation("INVOKED-OBJECT", methodFactory.newMethod("methodName", MethodFactory.NO_ARGUMENTS, void.class, EXCEPTION_TYPES)); throwAction = new ThrowAction(THROWABLE); }
public void testDoesNotAllowThrowingIncompatibleCheckedException() throws Throwable { Class<?>[] expectedExceptionTypes = {ExpectedExceptionType1.class, ExpectedExceptionType2.class}; Invocation incompatibleInvocation = new Invocation("INVOKED-OBJECT", methodFactory.newMethod("methodName", MethodFactory.NO_ARGUMENTS, void.class, expectedExceptionTypes)); try { throwAction.invoke(incompatibleInvocation); } catch (IllegalStateException ex) { String message = ex.getMessage(); for (int i = 0; i < expectedExceptionTypes.length; i++) { AssertThat.stringIncludes("should include name of expected exception types", expectedExceptionTypes[i].getName(), message); } AssertThat.stringIncludes("should include name of thrown exception type", THROWABLE.getClass().getName(), message); return; } fail("should have failed"); }
public void testGivesInformativeErrorMessageIfAttemptToThrowCheckedExceptionFromMethodWithNoExceptions() throws Throwable { Invocation incompatibleInvocation = new Invocation("INVOKED-OBJECT", methodFactory.newMethod("methodName", MethodFactory.NO_ARGUMENTS, void.class, MethodFactory.NO_EXCEPTIONS)); try { throwAction.invoke(incompatibleInvocation); } catch (IllegalStateException ex) { String message = ex.getMessage(); AssertThat.stringIncludes("should include name of thrown exception type", THROWABLE.getClass().getName(), message); AssertThat.stringIncludes("should describe that the method doesn't allow any exceptions", "no exceptions", message); return; } fail("should have failed"); }