Skip to content

Commit a2d3269

Browse files
committed
Change: fix matcher factories and expand examples test
1 parent 7cd5c4e commit a2d3269

File tree

2 files changed

+70
-11
lines changed

2 files changed

+70
-11
lines changed

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

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,86 @@ public ThrowsException(Matcher<? super Throwable> elementMatcher) {
2828
this.exceptionMatcher = elementMatcher;
2929
}
3030

31-
public static <U extends Runnable> ThrowsException<U> throwsException() {
31+
/**
32+
* Matcher for {@link Throwable} that expects that the Runnable throws an exception
33+
*
34+
* @param <T> type of the Runnable
35+
* @return The matcher.
36+
*/
37+
public static <T extends Runnable> ThrowsException<T> throwsException() {
3238
return new ThrowsException<>(instanceOf(Throwable.class));
3339
}
3440

35-
public static <U extends Runnable, V extends Throwable> ThrowsException<U> throwsException(V item) {
36-
return new ThrowsException<>(exceptionEqualTo(item));
41+
/**
42+
* Matcher for {@link Throwable} that expects that the Runnable throws an exception equal to the provided <code>throwable</code>
43+
*
44+
* @param <T> type of the Runnable
45+
* @param <U> type of the Throwable
46+
* @param throwable the Throwable instance against which examined exceptions are compared
47+
* @return The matcher.
48+
*/
49+
public static <T extends Runnable, U extends Throwable> ThrowsException<T> throwsException(U throwable) {
50+
return new ThrowsException<>(exceptionEqualTo(throwable));
3751
}
3852

39-
public static <U extends Runnable> ThrowsException<U> throwsException(Matcher<? super Throwable> exceptionMatcher) {
40-
return new ThrowsException<>(exceptionMatcher);
53+
/**
54+
* Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided <code>throwableClass</code> class
55+
*
56+
* @param <U> type of the Runnable
57+
* @param <T> type of the Throwable
58+
* @param throwableClass the Throwable class against which examined exceptions are compared
59+
* @return The matcher.
60+
*/
61+
public static <T extends Runnable, U extends Throwable> ThrowsException<T> throwsException(Class<U> throwableClass) {
62+
return new ThrowsException<>(instanceOf(throwableClass));
4163
}
4264

43-
public static <U extends Runnable, V extends Throwable> ThrowsException<U> throwsException(Class<V> item) {
44-
return new ThrowsException<>(instanceOf(item));
65+
/**
66+
* Matcher for {@link Throwable} that expects that the Runnable throws an exception with a message equal to the provided <code>message</code> class
67+
*
68+
* @param <T> type of the Runnable
69+
* @param message the String against which examined exception messages are compared
70+
* @return The matcher.
71+
*/
72+
public static <T extends Runnable> ThrowsException<T> throwsException(String message) {
73+
return new ThrowsException<>(withMessage(message));
4574
}
4675

47-
public static <U extends Runnable, V extends Throwable> ThrowsException<U> throwsException(Class<V> item, String message) {
48-
return new ThrowsException<>(allOf(instanceOf(item), withMessage(message)));
76+
/**
77+
* Matcher for {@link Throwable} that expects that the Runnable throws an exception matching provided <code>matcher</code>
78+
*
79+
* @param <T> type of the Runnable
80+
* @param matcher matcher to validate the exception
81+
* @return The matcher.
82+
*/
83+
public static <T extends Runnable> ThrowsException<T> throwsException(Matcher<? super Throwable> matcher) {
84+
return new ThrowsException<>(matcher);
4985
}
5086

51-
public static <U extends Runnable, V extends Throwable> ThrowsException<U> throwsException(Class<V> item, Matcher<? super Throwable> exceptionMatcher) {
52-
return new ThrowsException<>(allOf(instanceOf(item), exceptionMatcher));
87+
/**
88+
* 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>
89+
*
90+
* @param <T> type of the Runnable
91+
* @param <U> type of the Throwable
92+
* @param throwableClass the Throwable class against which examined exceptions are compared
93+
* @param message the String against which examined exception messages are compared
94+
* @return The matcher.
95+
*/
96+
public static <T extends Runnable, U extends Throwable> ThrowsException<T> throwsException(Class<U> throwableClass, String message) {
97+
return new ThrowsException<>(allOf(instanceOf(throwableClass), withMessage(message)));
98+
}
99+
100+
/**
101+
* 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>
102+
*
103+
* @param <U> type of the Runnable
104+
* @param <T> type of the Throwable
105+
* @param throwableClass the Throwable class against which examined exceptions are compared
106+
* @param matcher matcher to validate the exception
107+
* @return The matcher.
108+
*/
109+
public static <T extends Runnable, U extends Throwable> ThrowsException<T> throwsException(Class<U> throwableClass, Matcher<? super Throwable> matcher) {
110+
return new ThrowsException<>(allOf(instanceOf(throwableClass), matcher));
53111
}
54112

55113
@Override

hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public void examples() {
2525
assertThat(codeThatThrows, throwsException());
2626
assertThat(codeThatThrows, throwsException(boom));
2727
assertThat(codeThatThrows, throwsException(RuntimeException.class));
28+
assertThat(codeThatThrows, throwsException("boom"));
2829
assertThat(codeThatThrows, throwsException(withMessage("boom")));
2930
assertThat(codeThatThrows, throwsException(withMessage(containsString("oom"))));
3031
assertThat(codeThatThrows, throwsException(RuntimeException.class, "boom"));

0 commit comments

Comments
 (0)