From 69c64bde998ff6cc792675bc49947fb9a63008d6 Mon Sep 17 00:00:00 2001 From: "Marius B. Kotsbak" Date: Sat, 19 Jun 2010 04:23:38 +0200 Subject: [PATCH] Added support for verifying same exception is thrown as recorded. --- .../java/no/muda/jackbox/MethodRecording.java | 16 ++++- .../no/muda/jackbox/JackboxReplayTest.java | 65 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/src/main/java/no/muda/jackbox/MethodRecording.java b/src/main/java/no/muda/jackbox/MethodRecording.java index 31b2ba9..1ec536f 100644 --- a/src/main/java/no/muda/jackbox/MethodRecording.java +++ b/src/main/java/no/muda/jackbox/MethodRecording.java @@ -65,16 +65,30 @@ public void replay() throws Exception { JackboxAspect.setReplayingRecording(this); + boolean gotException = false; Object replayedResult = null; try { replayedResult = getMethod().invoke(replayInstance, arguments); } catch (InvocationTargetException e) { + if (getExceptionThrown() == null + || getExceptionThrown().getClass() != e.getCause().getClass()) { + String expected = (getExceptionThrown() == null) ? + "no exception" : getExceptionThrown().getClass().getName(); + throw new AssertionError("When replaying " + getMethod() + + " expected throwing of <" + expected + "> got <" + + e.getCause().getClass().getName() + ">"); + } + gotException = true; } JackboxAspect.clearReplayingRecording(); - if (!nullSafeEquals(replayedResult, getRecordedResult())) { + if (getExceptionThrown() != null && !gotException) { + throw new AssertionError("When replaying " + getMethod() + + " expected throwing of <" + getExceptionThrown() + ">, got no exception thrown"); + } + else if (!nullSafeEquals(replayedResult, getRecordedResult())) { throw new AssertionError("When replaying " + getMethod() + " expected <" + getRecordedResult() + "> got <" + replayedResult + ">"); diff --git a/src/test/java/no/muda/jackbox/JackboxReplayTest.java b/src/test/java/no/muda/jackbox/JackboxReplayTest.java index 7dd2d6f..9bb8ca9 100644 --- a/src/test/java/no/muda/jackbox/JackboxReplayTest.java +++ b/src/test/java/no/muda/jackbox/JackboxReplayTest.java @@ -43,6 +43,71 @@ public void shouldThrowExceptionIfReturnValueChanges() throws Exception { assertThat(threwException).describedAs("Should throw when return changes").isTrue(); } + @Test + public void shouldThrowExceptionIfRecordedExceptionIsNotThrown() throws Exception { + MethodRecording methodRecording = new MethodRecording(ExampleRecordedObject.class, + ExampleRecordedObject.class.getMethod("methodThatThrowsException", boolean.class), + new Object[] {false}); + methodRecording.setExceptionThrown(new IllegalArgumentException()); + + boolean threwException = false; + try { + methodRecording.replay(); + } catch (AssertionError e) { + assertThat(e.getMessage()) + .contains("expected throwing of ") + .contains("no exception") + .contains("methodThatThrowsException"); + // @TODO: include parameters in Assertion too + threwException = true; + } + + assertThat(threwException).describedAs("Should throw when thrown exception changes").isTrue(); + } + + @Test + public void shouldThrowExceptionIfThrowingAnExceptionWhenNotRecorded() throws Exception { + MethodRecording methodRecording = new MethodRecording(ExampleRecordedObject.class, + ExampleRecordedObject.class.getMethod("methodThatThrowsException", boolean.class), + new Object[] {true}); + + boolean threwException = false; + try { + methodRecording.replay(); + } catch (AssertionError e) { + assertThat(e.getMessage()) + .contains("expected throwing of ") + .contains("got ") + .contains("methodThatThrowsException"); + // @TODO: include parameters in Assertion too + threwException = true; + } + + assertThat(threwException).describedAs("Should throw when thrown exception changes").isTrue(); + } + + @Test + public void shouldThrowExceptionIfThrowingAnotherExceptionThanRecorded() throws Exception { + MethodRecording methodRecording = new MethodRecording(ExampleRecordedObject.class, + ExampleRecordedObject.class.getMethod("methodThatThrowsException", boolean.class), + new Object[] {true}); + methodRecording.setExceptionThrown(new NullPointerException()); + + boolean threwException = false; + try { + methodRecording.replay(); + } catch (AssertionError e) { + assertThat(e.getMessage()) + .contains("expected throwing of ") + .contains("got ") + .contains("methodThatThrowsException"); + // @TODO: include parameters in Assertion too + threwException = true; + } + + assertThat(threwException).describedAs("Should throw when thrown exception changes").isTrue(); + } + @Test public void shouldReplayDelegatedObject() throws Exception { String recordedReturnValueFromDependencyMethod = "foo bar baz";