Skip to content

Commit 0fc1b95

Browse files
committed
Change: fix aliases and matcher factories and expand examples test
1 parent 319e214 commit 0fc1b95

File tree

3 files changed

+79
-20
lines changed

3 files changed

+79
-20
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,7 +1999,7 @@ public static <T> Matcher<Optional<T>> optionalWithValue(Matcher<? super T> matc
19991999
* @return The matcher.
20002000
*/
20012001
public static <T extends Throwable> ThrowsExceptionEqualTo<T> exceptionEqualTo(T throwable) {
2002-
return new ThrowsExceptionEqualTo<>(throwable);
2002+
return ThrowsExceptionEqualTo.exceptionEqualTo(throwable);
20032003
}
20042004

20052005
/**
@@ -2013,7 +2013,7 @@ public static <T extends Throwable> ThrowsExceptionEqualTo<T> exceptionEqualTo(T
20132013
* @return The matcher.
20142014
*/
20152015
public static <T extends Throwable, U extends String> ThrowsExceptionWithMessage<T> withMessage(U message) {
2016-
return new ThrowsExceptionWithMessage<>(equalTo(message));
2016+
return ThrowsExceptionWithMessage.withMessage(message);
20172017
}
20182018

20192019
/**
@@ -2023,7 +2023,7 @@ public static <T extends Throwable, U extends String> ThrowsExceptionWithMessage
20232023
* @return The matcher.
20242024
*/
20252025
public static <T extends Runnable> ThrowsException<T> throwsException() {
2026-
return new ThrowsException<>(instanceOf(Throwable.class));
2026+
return ThrowsException.throwsException();
20272027
}
20282028

20292029
/**
@@ -2035,7 +2035,7 @@ public static <T extends Runnable> ThrowsException<T> throwsException() {
20352035
* @return The matcher.
20362036
*/
20372037
public static <T extends Runnable, U extends Throwable> ThrowsException<T> throwsException(U throwable) {
2038-
return new ThrowsException<>(exceptionEqualTo(throwable));
2038+
return ThrowsException.throwsException(throwable);
20392039
}
20402040

20412041
/**
@@ -2047,7 +2047,7 @@ public static <T extends Runnable, U extends Throwable> ThrowsException<T> throw
20472047
* @return The matcher.
20482048
*/
20492049
public static <T extends Runnable, U extends Throwable> ThrowsException<T> throwsException(Class<U> throwableClass) {
2050-
return new ThrowsException<>(instanceOf(throwableClass));
2050+
return ThrowsException.throwsException(throwableClass);
20512051
}
20522052

20532053
/**
@@ -2058,7 +2058,7 @@ public static <T extends Runnable, U extends Throwable> ThrowsException<T> throw
20582058
* @return The matcher.
20592059
*/
20602060
public static <T extends Runnable> ThrowsException<T> throwsException(String message) {
2061-
return new ThrowsException<>(withMessage(message));
2061+
return ThrowsException.throwsException(message);
20622062
}
20632063

20642064
/**
@@ -2069,7 +2069,7 @@ public static <T extends Runnable> ThrowsException<T> throwsException(String mes
20692069
* @return The matcher.
20702070
*/
20712071
public static <T extends Runnable> ThrowsException<T> throwsException(Matcher<? super Throwable> matcher) {
2072-
return new ThrowsException<>(matcher);
2072+
return ThrowsException.throwsException(matcher);
20732073
}
20742074

20752075
/**
@@ -2082,7 +2082,7 @@ public static <T extends Runnable> ThrowsException<T> throwsException(Matcher<?
20822082
* @return The matcher.
20832083
*/
20842084
public static <T extends Runnable, U extends Throwable> ThrowsException<T> throwsException(Class<U> throwableClass, String message) {
2085-
return new ThrowsException<>(allOf(instanceOf(throwableClass), withMessage(message)));
2085+
return ThrowsException.throwsException(throwableClass, message);
20862086
}
20872087

20882088
/**
@@ -2095,6 +2095,6 @@ public static <T extends Runnable, U extends Throwable> ThrowsException<T> throw
20952095
* @return The matcher.
20962096
*/
20972097
public static <T extends Runnable, U extends Throwable> ThrowsException<T> throwsException(Class<U> throwableClass, Matcher<? super Throwable> matcher) {
2098-
return new ThrowsException<>(allOf(instanceOf(throwableClass), matcher));
2098+
return ThrowsException.throwsException(throwableClass, matcher);
20992099
}
21002100
}

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)