|
16 | 16 | */ |
17 | 17 | public class ThrowsException<T extends Runnable> extends TypeSafeMatcher<T> { |
18 | 18 |
|
19 | | - private final Matcher<? super Throwable> exceptionMatcher; |
20 | | - private Throwable actualException; |
| 19 | + private final Matcher<? super Throwable> exceptionMatcher; |
| 20 | + private Throwable actualException; |
21 | 21 |
|
22 | | - /** |
23 | | - * Constructor, best called from one of the static factory methods. |
24 | | - * |
25 | | - * @param elementMatcher matches the expected element |
26 | | - */ |
27 | | - public ThrowsException(Matcher<? super Throwable> elementMatcher) { |
28 | | - this.exceptionMatcher = elementMatcher; |
29 | | - } |
| 22 | + /** |
| 23 | + * Constructor, best called from one of the static factory methods. |
| 24 | + * |
| 25 | + * @param elementMatcher matches the expected element |
| 26 | + */ |
| 27 | + public ThrowsException(Matcher<? super Throwable> elementMatcher) { |
| 28 | + this.exceptionMatcher = elementMatcher; |
| 29 | + } |
30 | 30 |
|
31 | | - public static <U extends Runnable> ThrowsException<U> throwsException() { |
32 | | - return new ThrowsException<>(instanceOf(Throwable.class)); |
33 | | - } |
| 31 | + public static <U extends Runnable> ThrowsException<U> throwsException() { |
| 32 | + return new ThrowsException<>(instanceOf(Throwable.class)); |
| 33 | + } |
34 | 34 |
|
35 | | - public static <U extends Runnable, V extends Throwable> ThrowsException<U> throwsException(V item) { |
36 | | - return new ThrowsException<>(exceptionEqualTo(item)); |
37 | | - } |
| 35 | + public static <U extends Runnable, V extends Throwable> ThrowsException<U> throwsException(V item) { |
| 36 | + return new ThrowsException<>(exceptionEqualTo(item)); |
| 37 | + } |
38 | 38 |
|
39 | | - public static <U extends Runnable> ThrowsException<U> throwsException(Matcher<? super Throwable> exceptionMatcher) { |
40 | | - return new ThrowsException<>(exceptionMatcher); |
41 | | - } |
| 39 | + public static <U extends Runnable> ThrowsException<U> throwsException(Matcher<? super Throwable> exceptionMatcher) { |
| 40 | + return new ThrowsException<>(exceptionMatcher); |
| 41 | + } |
42 | 42 |
|
43 | | - public static <U extends Runnable, V extends Throwable> ThrowsException<U> throwsException(Class<V> item) { |
44 | | - return new ThrowsException<>(instanceOf(item)); |
45 | | - } |
| 43 | + public static <U extends Runnable, V extends Throwable> ThrowsException<U> throwsException(Class<V> item) { |
| 44 | + return new ThrowsException<>(instanceOf(item)); |
| 45 | + } |
46 | 46 |
|
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))); |
49 | | - } |
| 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))); |
| 49 | + } |
50 | 50 |
|
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)); |
53 | | - } |
| 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)); |
| 53 | + } |
54 | 54 |
|
55 | | - @Override |
56 | | - public boolean matchesSafely(T runnable) { |
57 | | - try { |
58 | | - runnable.run(); |
59 | | - return false; |
60 | | - } catch (Throwable t) { |
61 | | - actualException = t; |
62 | | - return exceptionMatcher.matches(t); |
63 | | - } |
| 55 | + @Override |
| 56 | + public boolean matchesSafely(T runnable) { |
| 57 | + try { |
| 58 | + runnable.run(); |
| 59 | + return false; |
| 60 | + } catch (Throwable t) { |
| 61 | + actualException = t; |
| 62 | + return exceptionMatcher.matches(t); |
64 | 63 | } |
| 64 | + } |
65 | 65 |
|
66 | | - @Override |
67 | | - public void describeTo(Description description) { |
68 | | - description.appendText("a runnable throwing ").appendDescriptionOf(exceptionMatcher); |
69 | | - } |
| 66 | + @Override |
| 67 | + public void describeTo(Description description) { |
| 68 | + description.appendText("a runnable throwing ").appendDescriptionOf(exceptionMatcher); |
| 69 | + } |
70 | 70 |
|
71 | | - @Override |
72 | | - public void describeMismatchSafely(T runnable, Description mismatchDescription) { |
73 | | - if (actualException == null) { |
74 | | - mismatchDescription.appendText("the runnable didn't throw"); |
75 | | - return; |
76 | | - } |
77 | | - mismatchDescription.appendText("the runnable threw "); |
78 | | - exceptionMatcher.describeMismatch(actualException, mismatchDescription); |
| 71 | + @Override |
| 72 | + public void describeMismatchSafely(T runnable, Description mismatchDescription) { |
| 73 | + if (actualException == null) { |
| 74 | + mismatchDescription.appendText("the runnable didn't throw"); |
| 75 | + return; |
79 | 76 | } |
| 77 | + mismatchDescription.appendText("the runnable threw "); |
| 78 | + exceptionMatcher.describeMismatch(actualException, mismatchDescription); |
| 79 | + } |
80 | 80 | } |
0 commit comments