Skip to content

Commit 4cd878a

Browse files
committed
Change: add alias to new matchers in org.hamcrest.Matchers
1 parent e0221d7 commit 4cd878a

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

hamcrest/src/main/java/org/hamcrest/Matchers.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import org.hamcrest.collection.ArrayMatching;
44
import org.hamcrest.core.IsIterableContaining;
55
import org.hamcrest.core.StringRegularExpression;
6+
import org.hamcrest.exception.ThrowsException;
7+
import org.hamcrest.exception.ThrowsExceptionEqualTo;
8+
import org.hamcrest.exception.ThrowsExceptionWithMessage;
69
import org.hamcrest.optional.OptionalEmpty;
710
import org.hamcrest.optional.OptionalWithValue;
811
import org.hamcrest.text.IsEqualCompressingWhiteSpace;
@@ -2228,4 +2231,86 @@ public static <T> Matcher<Optional<T>> optionalWithValue(T value) {
22282231
public static <T> Matcher<Optional<T>> optionalWithValue(Matcher<? super T> matcher) {
22292232
return OptionalWithValue.optionalWithValue(matcher);
22302233
}
2234+
2235+
/**
2236+
* Matcher for {@link Throwable} that expects that the Runnable throws an exception
2237+
*
2238+
* @param <T> type of the Runnable
2239+
* @return The matcher.
2240+
*/
2241+
public static <T extends Runnable> ThrowsException<T> throwsException() {
2242+
return ThrowsException.throwsException();
2243+
}
2244+
2245+
/**
2246+
* Matcher for {@link Throwable} that expects that the Runnable throws an exception equal to the provided <code>throwable</code>
2247+
*
2248+
* @param <T> type of the Runnable
2249+
* @param <U> type of the Throwable
2250+
* @param throwable the Throwable instance against which examined exceptions are compared
2251+
* @return The matcher.
2252+
*/
2253+
public static <T extends Runnable, U extends Throwable> ThrowsException<T> throwsException(U throwable) {
2254+
return ThrowsException.throwsException(throwable);
2255+
}
2256+
2257+
/**
2258+
* Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided <code>throwableClass</code> class
2259+
*
2260+
* @param <U> type of the Runnable
2261+
* @param <T> type of the Throwable
2262+
* @param throwableClass the Throwable class against which examined exceptions are compared
2263+
* @return The matcher.
2264+
*/
2265+
public static <T extends Runnable, U extends Throwable> ThrowsException<T> throwsException(Class<U> throwableClass) {
2266+
return ThrowsException.throwsException(throwableClass);
2267+
}
2268+
2269+
/**
2270+
* Matcher for {@link Throwable} that expects that the Runnable throws an exception with a message equal to the provided <code>message</code> class
2271+
*
2272+
* @param <T> type of the Runnable
2273+
* @param message the String against which examined exception messages are compared
2274+
* @return The matcher.
2275+
*/
2276+
public static <T extends Runnable> ThrowsException<T> throwsException(String message) {
2277+
return ThrowsException.throwsException(message);
2278+
}
2279+
2280+
/**
2281+
* Matcher for {@link Throwable} that expects that the Runnable throws an exception matching provided <code>matcher</code>
2282+
*
2283+
* @param <T> type of the Runnable
2284+
* @param matcher matcher to validate the exception
2285+
* @return The matcher.
2286+
*/
2287+
public static <T extends Runnable> ThrowsException<T> throwsException(Matcher<? super Throwable> matcher) {
2288+
return ThrowsException.throwsException(matcher);
2289+
}
2290+
2291+
/**
2292+
* Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided <code>throwableClass</code> class and has a message equal to the provided <code>message</code>
2293+
*
2294+
* @param <T> type of the Runnable
2295+
* @param <U> type of the Throwable
2296+
* @param throwableClass the Throwable class against which examined exceptions are compared
2297+
* @param message the String against which examined exception messages are compared
2298+
* @return The matcher.
2299+
*/
2300+
public static <T extends Runnable, U extends Throwable> ThrowsException<T> throwsException(Class<U> throwableClass, String message) {
2301+
return ThrowsException.throwsException(throwableClass, message);
2302+
}
2303+
2304+
/**
2305+
* Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided <code>throwableClass</code> class and matches the provided <code>matcher</code>
2306+
*
2307+
* @param <U> type of the Runnable
2308+
* @param <T> type of the Throwable
2309+
* @param throwableClass the Throwable class against which examined exceptions are compared
2310+
* @param matcher matcher to validate the exception
2311+
* @return The matcher.
2312+
*/
2313+
public static <T extends Runnable, U extends Throwable> ThrowsException<T> throwsException(Class<U> throwableClass, Matcher<? super Throwable> matcher) {
2314+
return ThrowsException.throwsException(throwableClass, matcher);
2315+
}
22312316
}

hamcrest/src/main/java/org/hamcrest/exception/ThrowsException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class ThrowsException<T extends Runnable> extends TypeSafeMatcher<T> {
2424
*
2525
* @param elementMatcher matches the expected element
2626
*/
27-
ThrowsException(Matcher<? super Throwable> elementMatcher) {
27+
public ThrowsException(Matcher<? super Throwable> elementMatcher) {
2828
this.exceptionMatcher = elementMatcher;
2929
}
3030

hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionEqualTo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ThrowsExceptionEqualTo<T extends Throwable> extends TypeSafeDiagnos
1616
*
1717
* @param expectedException the expected exception.
1818
*/
19-
ThrowsExceptionEqualTo(T expectedException) {
19+
public ThrowsExceptionEqualTo(T expectedException) {
2020
this.expectedException = expectedException;
2121
}
2222

hamcrest/src/main/java/org/hamcrest/exception/ThrowsExceptionWithMessage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class ThrowsExceptionWithMessage<T extends Throwable> extends TypeSafeDia
1919
*
2020
* @param messageMatcher matches the exception message
2121
*/
22-
ThrowsExceptionWithMessage(Matcher<? super String> messageMatcher) {
22+
public ThrowsExceptionWithMessage(Matcher<? super String> messageMatcher) {
2323
this.messageMatcher = messageMatcher;
2424
}
2525

0 commit comments

Comments
 (0)