Skip to content

Commit

Permalink
Added support for verifying same exception is thrown as recorded.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkotsbak committed Jun 19, 2010
1 parent 287c330 commit 69c64bd
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/main/java/no/muda/jackbox/MethodRecording.java
Expand Up @@ -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 + ">");
Expand Down
65 changes: 65 additions & 0 deletions src/test/java/no/muda/jackbox/JackboxReplayTest.java
Expand Up @@ -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 <java.lang.IllegalArgumentException>")
.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 <no exception>")
.contains("got <java.lang.IllegalArgumentException>")
.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 <java.lang.NullPointerException>")
.contains("got <java.lang.IllegalArgumentException>")
.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";
Expand Down

0 comments on commit 69c64bd

Please sign in to comment.