-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add matcher for thrown exceptions in
Runnable
(#423)
- Loading branch information
Showing
3 changed files
with
254 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package org.hamcrest.exception; | ||
|
||
import org.hamcrest.Description; | ||
import org.hamcrest.Matcher; | ||
import org.hamcrest.TypeSafeDiagnosingMatcher; | ||
import org.hamcrest.core.IsInstanceOf; | ||
|
||
import static org.hamcrest.core.IsAnything.anything; | ||
import static org.hamcrest.core.IsEqual.equalTo; | ||
|
||
/** | ||
* Tests if a Runnable throws a matching exception. | ||
* | ||
* @param <T> the type of the matched Runnable | ||
*/ | ||
public class ThrowsException<T extends Runnable> extends TypeSafeDiagnosingMatcher<T> { | ||
private final IsInstanceOf classMatcher; | ||
private final Matcher<? super String> messageMatcher; | ||
|
||
public ThrowsException(IsInstanceOf classMatcher, Matcher<? super String> messageMatcher) { | ||
this.classMatcher = classMatcher; | ||
this.messageMatcher = messageMatcher; | ||
} | ||
|
||
public static <T extends Runnable> ThrowsException<T> throwsException() { | ||
return throwsException(Throwable.class); | ||
} | ||
|
||
public static <T extends Runnable, U extends Throwable> ThrowsException<T> throwsException(Class<U> throwableClass) { | ||
return new ThrowsException<>(new IsInstanceOf(throwableClass), anything("<anything>")); | ||
} | ||
|
||
public static <T extends Runnable, U extends Throwable> ThrowsException<T> throwsException(Class<U> throwableClass, String exactMessage) { | ||
return throwsException(throwableClass, equalTo(exactMessage)); | ||
} | ||
|
||
public static <T extends Runnable, U extends Throwable> ThrowsException<T> throwsException(Class<U> throwableClass, Matcher<String> messageMatcher) { | ||
return new ThrowsException<>(new IsInstanceOf(throwableClass), messageMatcher); | ||
} | ||
|
||
public static <T extends Runnable> ThrowsException<T> throwsExceptionWithMessage(String exactMessage) { | ||
return throwsException(Throwable.class, equalTo(exactMessage)); | ||
} | ||
|
||
public static <T extends Runnable> ThrowsException<T> throwsExceptionWithMessage(Matcher<String> messageMatcher) { | ||
return throwsException(Throwable.class, messageMatcher); | ||
} | ||
|
||
@Override | ||
protected boolean matchesSafely(T runnable, Description mismatchDescription) { | ||
try { | ||
runnable.run(); | ||
mismatchDescription.appendText("the runnable didn't throw"); | ||
return false; | ||
} catch (Throwable t) { | ||
boolean classMatches = classMatcher.matches(t); | ||
if (!classMatches) { | ||
mismatchDescription.appendText("thrown exception class was ").appendText(t.getClass().getName()); | ||
} | ||
|
||
boolean messageMatches = messageMatcher.matches(t.getMessage()); | ||
if (!messageMatches) { | ||
if (!classMatches) { | ||
mismatchDescription.appendText(" and the "); | ||
} | ||
mismatchDescription.appendText("thrown exception message "); | ||
messageMatcher.describeMismatch(t.getMessage(), mismatchDescription); | ||
} | ||
|
||
return classMatches && messageMatches; | ||
} | ||
} | ||
|
||
@Override | ||
public void describeTo(Description description) { | ||
description | ||
.appendText("a runnable throwing ").appendDescriptionOf(classMatcher) | ||
.appendText(" with message ").appendDescriptionOf(messageMatcher); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org.hamcrest.exception; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.exception.ThrowsException.throwsException; | ||
import static org.hamcrest.test.MatcherAssertions.*; | ||
|
||
public final class ThrowsExceptionTest { | ||
|
||
public static void throwIllegalArgumentException() { | ||
throw new IllegalArgumentException("Boom!"); | ||
} | ||
|
||
public static void throwNullPointerException() { | ||
throw new NullPointerException("Boom!"); | ||
} | ||
|
||
@Test | ||
public void examples() { | ||
assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException()); | ||
assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException(RuntimeException.class)); | ||
assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException(RuntimeException.class, "Boom!")); | ||
assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException(RuntimeException.class, containsString("Boo"))); | ||
} | ||
|
||
@Test | ||
public void evaluatesToTrueIfRunnableThrowsExpectedExceptionWithMatchingMessage() { | ||
assertMatches( | ||
throwsException(IllegalArgumentException.class, "Boom!"), | ||
ThrowsExceptionTest::throwIllegalArgumentException | ||
); | ||
|
||
assertDescription( | ||
"a runnable throwing an instance of java.lang.IllegalArgumentException with message \"Boom!\"", | ||
throwsException(IllegalArgumentException.class, "Boom!") | ||
); | ||
|
||
assertMismatchDescription( | ||
"thrown exception message was \"Boom!\"", | ||
throwsException(IllegalArgumentException.class, "Bang!"), | ||
(Runnable) ThrowsExceptionTest::throwIllegalArgumentException | ||
); | ||
|
||
assertMismatchDescription( | ||
"thrown exception class was java.lang.NullPointerException", | ||
throwsException(IllegalArgumentException.class, "Boom!"), | ||
(Runnable) ThrowsExceptionTest::throwNullPointerException | ||
); | ||
|
||
assertMismatchDescription( | ||
"the runnable didn't throw", | ||
throwsException(IllegalArgumentException.class, "Boom!"), | ||
(Runnable) () -> { | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
public void evaluatesToTrueIfRunnableThrowsExceptionExtendingTheExpectedExceptionWithMatchingMessage() { | ||
assertMatches( | ||
throwsException(IllegalArgumentException.class, "Boom!"), | ||
ThrowsExceptionTest::throwIllegalArgumentException | ||
); | ||
} | ||
|
||
@Test | ||
public void evaluatesToTrueIfRunnableThrowsExceptionWithMatchingMessage() { | ||
assertMatches( | ||
throwsException(IllegalArgumentException.class, containsString("Boo")), | ||
ThrowsExceptionTest::throwIllegalArgumentException | ||
); | ||
|
||
assertDescription( | ||
"a runnable throwing an instance of java.lang.IllegalArgumentException with message a string containing \"Boo\"", | ||
throwsException(IllegalArgumentException.class, containsString("Boo")) | ||
); | ||
|
||
assertMismatchDescription( | ||
"thrown exception class was java.lang.NullPointerException", | ||
throwsException(IllegalArgumentException.class, containsString("Boo")), | ||
(Runnable) ThrowsExceptionTest::throwNullPointerException | ||
); | ||
} | ||
} |