Skip to content

Commit

Permalink
Reused HasWrongExpectedException instead of own defined class
Browse files Browse the repository at this point in the history
(they were the same) and relocated the changes to a better location
in the file which is more inline with the rest of the file. And,
of course, removed the superfluous brackets.
  • Loading branch information
arjenw committed Jan 4, 2012
2 parents 2cd3988 + 9995466 commit bd6dc3a
Showing 1 changed file with 12 additions and 25 deletions.
Expand Up @@ -57,50 +57,37 @@ public void throwsNullPointerException() {
}
}

public static class ExpectedExceptionClause {
@Rule
public ExpectedException thrown= ExpectedException.none();

@Test
public void throwsNullPointerException() {
thrown.expect(NullPointerException.class);
throw new IllegalArgumentException();
}
@Test
public void unExpectedExceptionFails() {
assertThat(
testResult(HasWrongExpectedException.class),
hasSingleFailureContaining("Expected: an instance of java.lang.NullPointerException"));
}

public static class CauseMatcher extends TypeSafeMatcher<Result> {
private final Class<? extends Throwable> causeClass;

public CauseMatcher(final Class<? extends Throwable> causeClass) {
public CauseMatcher(Class<? extends Throwable> causeClass) {
this.causeClass= causeClass;
}

public void describeTo(final Description description) {
description.appendText("cause an instanceof ").appendText(causeClass.getName());
public void describeTo(Description description) {
description.appendText("cause being an instanceof ").appendText(causeClass.getName());
}

@Override
public boolean matchesSafely(final Result item) {
public boolean matchesSafely(Result item) {
for(Failure f: item.getFailures())
{
return CoreMatchers.instanceOf(causeClass).matches(f.getException().getCause());
}
return false;
}
}

@Test
public void expectedExceptionClauseExists() {
assertThat(new JUnitCore().run(ExpectedExceptionClause.class),
assertThat(new JUnitCore().run(HasWrongExpectedException.class),
new CauseMatcher(IllegalArgumentException.class));
}

@Test
public void unExpectedExceptionFails() {
assertThat(
testResult(HasWrongExpectedException.class),
hasSingleFailureContaining("Expected: an instance of java.lang.NullPointerException"));
}
}

public static class HasWrongMessage {
@Rule
Expand Down

0 comments on commit bd6dc3a

Please sign in to comment.