private ExpectationError mismatchDescribing(final ExpectationError e) { ExpectationError filledIn = new ExpectationError(e.getMessage(), new SelfDescribing() { public void describeTo(Description description) { describeMismatch(e.invocation, description); } }, e.invocation); filledIn.setStackTrace(e.getStackTrace()); return filledIn; }
public Object invoke(Invocation invocation) throws Throwable { if (iterator.hasNext()) { return iterator.next().invoke(invocation); } throw ExpectationError.unexpected("no more actions available", invocation); }
public void describeTo(Description description) { description.appendText(getMessage()); if (invocation != null) { description.appendText(": "); invocation.describeTo(description); } if (expectations != null) { description.appendText("\n"); expectations.describeTo(description); } } }
public static ExpectationError notAllSatisfied(SelfDescribing expectations) { return new ExpectationError("not all expectations were satisfied", expectations, null); }
/** * Fails the test if there are any expectations that have not been met. */ public void assertIsSatisfied() { if (firstError != null) { throw firstError; } else if (!dispatcher.isSatisfied()) { throw expectationErrorTranslator.translate( ExpectationError.notAllSatisfied(this)); } }
private Object dispatch(Invocation invocation) throws Throwable { if (firstError != null) { throw firstError; } try { Object result = dispatcher.dispatch(invocation); actualInvocations.add(invocation); return result; } catch (ExpectationError e) { firstError = expectationErrorTranslator.translate(mismatchDescribing(e)); firstError.setStackTrace(e.getStackTrace()); throw firstError; } catch (Throwable t) { actualInvocations.add(invocation); throw t; } }
public void testErrorMessagesIncludeCurrentStates() { readiness.startsAs("ethelred"); States fruitiness = context.states("fruitiness"); fruitiness.startsAs("apple"); context.checking(new Expectations() {{ allowing (mock).method1(); when(readiness.is("ready")); }}); try { mock.method1(); fail("should have thrown ExpectationError"); } catch (ExpectationError e) { String message = e.toString(); AssertThat.stringIncludes("should describe readiness state", "readiness is ethelred", message); AssertThat.stringIncludes("should describe fruitiness state", "fruitiness is apple", message); } }
@SuppressWarnings("cast") // Eclipse gives warning if there is a cast and if there is not! public void testFailsIfInvokedMoreTimesThanThereAreActionsInTheSequence() throws Throwable { MockAction[] actions = new MockAction[]{new MockAction(), new MockAction()}; ActionSequence sequence = new ActionSequence((Action[])actions); for (int i = 0; i < actions.length; i++) sequence.invoke(invocation); try { sequence.invoke(invocation); fail("should have thrown IllegalStateException"); } catch (ExpectationError ex) { AssertThat.stringIncludes("should describe error", "no more actions", ex.getMessage()); return; } }
public static ExpectationError unexpected(String message, Invocation invocation) { return new ExpectationError(message, null, invocation); }
public Object dispatch(Invocation invocation) throws Throwable { for (Expectation expectation : expectations) { if (expectation.matches(invocation)) { return expectation.invoke(invocation); } } throw ExpectationError.unexpected("unexpected invocation", invocation); }
public void testReturnsTheErrorAsItsOwnTranslation() { ExpectationError e = ExpectationError.unexpected(null, null); assertSame(e, IdentityExpectationErrorTranslator.INSTANCE.translate(e)); } }