|
3 | 3 | import org.hamcrest.collection.ArrayMatching;
|
4 | 4 | import org.hamcrest.core.IsIterableContaining;
|
5 | 5 | import org.hamcrest.core.StringRegularExpression;
|
| 6 | +import org.hamcrest.exception.ThrowsException; |
6 | 7 | import org.hamcrest.optional.OptionalEmpty;
|
7 | 8 | import org.hamcrest.optional.OptionalWithValue;
|
8 | 9 | import org.hamcrest.text.IsEqualCompressingWhiteSpace;
|
@@ -1972,21 +1973,21 @@ public static Matcher<CharSequence> hasLength(org.hamcrest.Matcher<? super java.
|
1972 | 1973 | return org.hamcrest.text.CharSequenceLength.hasLength(lengthMatcher);
|
1973 | 1974 | }
|
1974 | 1975 |
|
1975 |
| - /** |
1976 |
| - * Creates a matcher of {@link CharSequence} that matches when a char sequence has the length |
1977 |
| - * of the specified <code>argument</code>. |
1978 |
| - * For example: |
1979 |
| - * |
1980 |
| - * <pre> |
1981 |
| - * assertThat("text", length(4)) |
1982 |
| - * </pre> |
1983 |
| - * |
1984 |
| - * @param length the expected length of the string |
1985 |
| - * @return The matcher. |
1986 |
| - */ |
1987 |
| - public static Matcher<CharSequence> hasLength(int length) { |
1988 |
| - return org.hamcrest.text.CharSequenceLength.hasLength(length); |
1989 |
| - } |
| 1976 | + /** |
| 1977 | + * Creates a matcher of {@link CharSequence} that matches when a char sequence has the length |
| 1978 | + * of the specified <code>argument</code>. |
| 1979 | + * For example: |
| 1980 | + * |
| 1981 | + * <pre> |
| 1982 | + * assertThat("text", length(4)) |
| 1983 | + * </pre> |
| 1984 | + * |
| 1985 | + * @param length the expected length of the string |
| 1986 | + * @return The matcher. |
| 1987 | + */ |
| 1988 | + public static Matcher<CharSequence> hasLength(int length) { |
| 1989 | + return org.hamcrest.text.CharSequenceLength.hasLength(length); |
| 1990 | + } |
1990 | 1991 |
|
1991 | 1992 | /**
|
1992 | 1993 | * Creates a matcher that matches any examined object whose <code>toString</code> method
|
@@ -2228,4 +2229,76 @@ public static <T> Matcher<Optional<T>> optionalWithValue(T value) {
|
2228 | 2229 | public static <T> Matcher<Optional<T>> optionalWithValue(Matcher<? super T> matcher) {
|
2229 | 2230 | return OptionalWithValue.optionalWithValue(matcher);
|
2230 | 2231 | }
|
| 2232 | + |
| 2233 | + /** |
| 2234 | + * Matcher for {@link Throwable} that expects that the Runnable throws an exception |
| 2235 | + * |
| 2236 | + * @param <T> type of the Runnable |
| 2237 | + * @return The matcher. |
| 2238 | + */ |
| 2239 | + public static <T extends Runnable> ThrowsException<T> throwsException() { |
| 2240 | + return ThrowsException.throwsException(); |
| 2241 | + } |
| 2242 | + |
| 2243 | + /** |
| 2244 | + * Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided <code>throwableClass</code> class |
| 2245 | + * |
| 2246 | + * @param <U> type of the Runnable |
| 2247 | + * @param <T> type of the Throwable |
| 2248 | + * @param throwableClass the Throwable class against which examined exceptions are compared |
| 2249 | + * @return The matcher. |
| 2250 | + */ |
| 2251 | + public static <T extends Runnable, U extends Throwable> ThrowsException<T> throwsException(Class<U> throwableClass) { |
| 2252 | + return ThrowsException.throwsException(throwableClass); |
| 2253 | + } |
| 2254 | + |
| 2255 | + /** |
| 2256 | + * 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> |
| 2257 | + * |
| 2258 | + * @param <T> type of the Runnable |
| 2259 | + * @param <U> type of the Throwable |
| 2260 | + * @param throwableClass the Throwable class against which examined exceptions are compared |
| 2261 | + * @param message the String against which examined exception messages are compared |
| 2262 | + * @return The matcher. |
| 2263 | + */ |
| 2264 | + public static <T extends Runnable, U extends Throwable> ThrowsException<T> throwsException(Class<U> throwableClass, String message) { |
| 2265 | + return ThrowsException.throwsException(throwableClass, message); |
| 2266 | + } |
| 2267 | + |
| 2268 | + /** |
| 2269 | + * Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided <code>throwableClass</code> class and has a message matching the provided <code>messageMatcher</code> |
| 2270 | + * |
| 2271 | + * @param <T> type of the Runnable |
| 2272 | + * @param <U> type of the Throwable |
| 2273 | + * @param throwableClass the Throwable class against which examined exceptions are compared |
| 2274 | + * @param messageMatcher matcher to validate exception's message |
| 2275 | + * @return The matcher. |
| 2276 | + */ |
| 2277 | + public static <T extends Runnable, U extends Throwable> ThrowsException<T> throwsException(Class<U> throwableClass, Matcher<String> messageMatcher) { |
| 2278 | + return ThrowsException.throwsException(throwableClass, messageMatcher); |
| 2279 | + } |
| 2280 | + |
| 2281 | + /** |
| 2282 | + * Matcher for {@link Throwable} that expects that the Runnable throws an exception with a message equal to the provided <code>message</code> |
| 2283 | + * |
| 2284 | + * @param <T> type of the Runnable |
| 2285 | + * @param <U> type of the Throwable |
| 2286 | + * @param message the String against which examined exception messages are compared |
| 2287 | + * @return The matcher. |
| 2288 | + */ |
| 2289 | + public static <T extends Runnable, U extends Throwable> ThrowsException<T> throwsExceptionWithMessage(String message) { |
| 2290 | + return ThrowsException.throwsExceptionWithMessage(message); |
| 2291 | + } |
| 2292 | + |
| 2293 | + /** |
| 2294 | + * Matcher for {@link Throwable} that expects that the Runnable throws an exception with a message matching the provided <code>messageMatcher</code> |
| 2295 | + * |
| 2296 | + * @param <T> type of the Runnable |
| 2297 | + * @param <U> type of the Throwable |
| 2298 | + * @param messageMatcher matcher to validate exception's message |
| 2299 | + * @return The matcher. |
| 2300 | + */ |
| 2301 | + public static <T extends Runnable, U extends Throwable> ThrowsException<T> throwsExceptionWithMessage(Matcher<String> messageMatcher) { |
| 2302 | + return ThrowsException.throwsExceptionWithMessage(messageMatcher); |
| 2303 | + } |
2231 | 2304 | }
|
0 commit comments