diff --git a/hamcrest/src/main/java/org/hamcrest/Matchers.java b/hamcrest/src/main/java/org/hamcrest/Matchers.java index dcc88366..60d8256a 100644 --- a/hamcrest/src/main/java/org/hamcrest/Matchers.java +++ b/hamcrest/src/main/java/org/hamcrest/Matchers.java @@ -2234,7 +2234,7 @@ public static Matcher> optionalWithValue(Matcher matc } /** - * Matcher for {@link Throwable} that expects that the Runnable throws an exception + * Matcher for {@link Runnable} that expects an exception to be thrown * * @param type of the Runnable * @return The matcher. @@ -2297,11 +2297,10 @@ public static Matcher throwsExcepti * Matcher for {@link Throwable} that expects that the Runnable throws an exception with a message equal to the provided message * * @param type of the Runnable - * @param type of the Throwable * @param message the String against which examined exception messages are compared * @return The matcher. */ - public static Matcher throwsExceptionWithMessage(String message) { + public static Matcher throwsExceptionWithMessage(String message) { return ThrowsException.throwsExceptionWithMessage(message); } @@ -2309,11 +2308,10 @@ public static Matcher throwsExcepti * Matcher for {@link Throwable} that expects that the Runnable throws an exception with a message matching the provided messageMatcher * * @param type of the Runnable - * @param type of the Throwable * @param messageMatcher matcher to validate exception's message * @return The matcher. */ - public static Matcher throwsExceptionWithMessage(Matcher messageMatcher) { + public static Matcher throwsExceptionWithMessage(Matcher messageMatcher) { return ThrowsException.throwsExceptionWithMessage(messageMatcher); } } diff --git a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java index 91f5fec4..aebb96b7 100644 --- a/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java +++ b/hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java @@ -17,35 +17,100 @@ public class ThrowsException extends TypeSafeDiagnosingMatch private final IsInstanceOf classMatcher; private final Matcher messageMatcher; + /** + * Constructor, best called from one of the static {@link #throwsException()} methods. + * @param classMatcher the matcher for the type of the exception + * @param messageMatcher the matcher for the exception message + */ public ThrowsException(IsInstanceOf classMatcher, Matcher messageMatcher) { this.classMatcher = classMatcher; this.messageMatcher = messageMatcher; } + /** + * Matcher for {@link Runnable} that expects an exception to be thrown + * + * @param type of the Runnable + * @return The matcher. + */ public static Matcher throwsException() { return throwsException(Throwable.class); } + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception equal + * to the provided throwable + * + * @param type of the Runnable + * @param type of the Throwable + * @param throwable the Throwable class against which examined exceptions are compared + * @return The matcher. + */ public static Matcher throwsException(U throwable) { return throwsException(throwable.getClass(), throwable.getMessage()); } + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception of the + * provided throwableClass class + * + * @param type of the Runnable + * @param type of the Throwable + * @param throwableClass the Throwable class against which examined exceptions are compared + * @return The matcher. + */ public static Matcher throwsException(Class throwableClass) { return new ThrowsException<>(new IsInstanceOf(throwableClass), anything("")); } + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception of the + * provided throwableClass class and has a message equal to the provided + * message + * + * @param type of the Runnable + * @param type of the Throwable + * @param throwableClass the Throwable class against which examined exceptions are compared + * @param exactMessage the String against which examined exception messages are compared + * @return The matcher. + */ public static Matcher throwsException(Class throwableClass, String exactMessage) { return throwsException(throwableClass, equalTo(exactMessage)); } + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided + * throwableClass class and has a message matching the provided + * messageMatcher + * + * @param type of the Runnable + * @param type of the Throwable + * @param throwableClass the Throwable class against which examined exceptions are compared + * @param messageMatcher matcher to validate exception's message + * @return The matcher. + */ public static Matcher throwsException(Class throwableClass, Matcher messageMatcher) { return new ThrowsException<>(new IsInstanceOf(throwableClass), messageMatcher); } + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception with a message equal to the provided message + * + * @param type of the Runnable + * @param exactMessage the String against which examined exception messages are compared + * @return The matcher. + */ public static Matcher throwsExceptionWithMessage(String exactMessage) { return throwsException(Throwable.class, equalTo(exactMessage)); } + /** + * Matcher for {@link Throwable} that expects that the Runnable throws an exception with a message matching the provided messageMatcher + * + * @param type of the Runnable + * @param messageMatcher matcher to validate exception's message + * @return The matcher. + */ public static Matcher throwsExceptionWithMessage(Matcher messageMatcher) { return throwsException(Throwable.class, messageMatcher); }