Skip to content

Commit 5058b28

Browse files
committed
#329 Updated fix for Java 1.7
1 parent 7844e8d commit 5058b28

File tree

3 files changed

+55
-14
lines changed

3 files changed

+55
-14
lines changed

hamcrest/src/main/java/org/hamcrest/Executable.java

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.hamcrest;
22

3-
@FunctionalInterface
43
public interface Executable {
54

65
void execute() throws Throwable;

hamcrest/src/main/java/org/hamcrest/Throws.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public final class Throws<T extends Throwable> implements SelfDescribing {
1010

1111
private final Matcher<? super T> matcher;
1212

13-
public Throws(Matcher<? super T> throwableMatcher) {
13+
public Throws(final Matcher<? super T> throwableMatcher) {
1414
requireNonNull(throwableMatcher);
1515
this.matcher = new BaseMatcher<T>() {
1616

@@ -46,7 +46,7 @@ public void describeMismatch(Description description) {
4646
description.appendText("did not throw");
4747
}
4848

49-
public static <T extends Throwable> Matcher<T> withMessage(Matcher<String> messageMatcher) {
49+
public static <T extends Throwable> Matcher<T> withMessage(final Matcher<String> messageMatcher) {
5050
return new TypeSafeMatcher<T>() {
5151

5252
@Override
@@ -68,7 +68,7 @@ protected void describeMismatchSafely(T item, Description mismatchDescription) {
6868
};
6969
}
7070

71-
public static <T extends Throwable> Matcher<T> becauseOf(Matcher<? extends Throwable> causeMatcher) {
71+
public static <T extends Throwable> Matcher<T> becauseOf(final Matcher<? extends Throwable> causeMatcher) {
7272
return new TypeSafeMatcher<T>() {
7373

7474
@Override

hamcrest/src/test/java/org/hamcrest/MatcherAssertTest.java

+52-10
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ public void describeMismatch(Object item, Description mismatchDescription) {
103103
@Test public void
104104
throwableIsOfMatchingInstance() {
105105
assertThat(
106-
() -> { throw new IllegalStateException(); },
106+
new Executable() {
107+
@Override
108+
public void execute() {
109+
throw new IllegalStateException();
110+
}
111+
},
107112
throwsInstanceOf(IllegalStateException.class)
108113
);
109114
}
@@ -115,7 +120,12 @@ public void describeMismatch(Object item, Description mismatchDescription) {
115120
+ " but: threw but <java.lang.IllegalStateException> is a java.lang.IllegalStateException";
116121
try {
117122
assertThat(
118-
() -> { throw new IllegalStateException(); },
123+
new Executable() {
124+
@Override
125+
public void execute() {
126+
throw new IllegalStateException();
127+
}
128+
},
119129
throwsInstanceOf(IOException.class)
120130
);
121131
fail("should have failed");
@@ -128,7 +138,12 @@ public void describeMismatch(Object item, Description mismatchDescription) {
128138
@Test public void
129139
throwableHasMatchingMessage() {
130140
assertThat(
131-
() -> { throw new Exception("message"); },
141+
new Executable() {
142+
@Override
143+
public void execute() throws Exception {
144+
throw new Exception("message");
145+
}
146+
},
132147
doesThrow(withMessage(equalTo("message")))
133148
);
134149
}
@@ -140,7 +155,12 @@ public void describeMismatch(Object item, Description mismatchDescription) {
140155
+ " but: threw but message was \"actual message\"";
141156
try {
142157
assertThat(
143-
() -> { throw new Exception("actual message"); },
158+
new Executable() {
159+
@Override
160+
public void execute() throws Exception {
161+
throw new Exception("actual message");
162+
}
163+
},
144164
doesThrow(withMessage("expected message"))
145165
);
146166
fail("should have failed");
@@ -157,7 +177,12 @@ public void describeMismatch(Object item, Description mismatchDescription) {
157177
+ endLine + " but: did not throw";
158178
try {
159179
assertThat(
160-
() -> {}, // Do nothing
180+
new Executable() {
181+
@Override
182+
public void execute() {
183+
// Do nothing
184+
}
185+
},
161186
throwsInstanceOf(NoSuchMethodError.class)
162187
);
163188
fail("should have failed");
@@ -169,9 +194,15 @@ public void describeMismatch(Object item, Description mismatchDescription) {
169194

170195
@Test public void
171196
throwableCauseMatches() {
197+
Matcher<? extends Throwable> instanceOfMatcher = instanceOf(XMLStreamException.class);
172198
assertThat(
173-
() -> { throw new RuntimeException(new XMLStreamException()); },
174-
doesThrow(becauseOf(instanceOf(XMLStreamException.class)))
199+
new Executable() {
200+
@Override
201+
public void execute() {
202+
throw new RuntimeException(new XMLStreamException());
203+
}
204+
},
205+
doesThrow(becauseOf(instanceOfMatcher))
175206
);
176207
}
177208

@@ -181,9 +212,15 @@ public void describeMismatch(Object item, Description mismatchDescription) {
181212
String expectedMessage = endLine + "Expected: throws because of an instance of java.lang.NullPointerException"
182213
+ endLine + " but: threw but cause <java.lang.IllegalArgumentException> is a java.lang.IllegalArgumentException";
183214
try {
215+
Matcher<? extends Throwable> instanceOfMatcher = instanceOf(NullPointerException.class);
184216
assertThat(
185-
() -> { throw new RuntimeException(new IllegalArgumentException()); },
186-
doesThrow(becauseOf(instanceOf(NullPointerException.class)))
217+
new Executable() {
218+
@Override
219+
public void execute() {
220+
throw new RuntimeException(new IllegalArgumentException());
221+
}
222+
},
223+
doesThrow(becauseOf(instanceOfMatcher))
187224
);
188225
fail("should have failed");
189226
}
@@ -201,7 +238,12 @@ public void describeMismatch(Object item, Description mismatchDescription) {
201238
try {
202239
assertThat(
203240
"Custom message",
204-
() -> { throw new IllegalArgumentException(); },
241+
new Executable() {
242+
@Override
243+
public void execute() {
244+
throw new IllegalArgumentException();
245+
}
246+
},
205247
throwsInstanceOf(NullPointerException.class)
206248
);
207249
fail("should have failed");

0 commit comments

Comments
 (0)