Skip to content

Commit

Permalink
hamcrest#329 Updated fix for Java 1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdemaeyer committed Feb 21, 2021
1 parent 7844e8d commit 5058b28
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
1 change: 0 additions & 1 deletion hamcrest/src/main/java/org/hamcrest/Executable.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.hamcrest;

@FunctionalInterface
public interface Executable {

void execute() throws Throwable;
Expand Down
6 changes: 3 additions & 3 deletions hamcrest/src/main/java/org/hamcrest/Throws.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public final class Throws<T extends Throwable> implements SelfDescribing {

private final Matcher<? super T> matcher;

public Throws(Matcher<? super T> throwableMatcher) {
public Throws(final Matcher<? super T> throwableMatcher) {
requireNonNull(throwableMatcher);
this.matcher = new BaseMatcher<T>() {

Expand Down Expand Up @@ -46,7 +46,7 @@ public void describeMismatch(Description description) {
description.appendText("did not throw");
}

public static <T extends Throwable> Matcher<T> withMessage(Matcher<String> messageMatcher) {
public static <T extends Throwable> Matcher<T> withMessage(final Matcher<String> messageMatcher) {
return new TypeSafeMatcher<T>() {

@Override
Expand All @@ -68,7 +68,7 @@ protected void describeMismatchSafely(T item, Description mismatchDescription) {
};
}

public static <T extends Throwable> Matcher<T> becauseOf(Matcher<? extends Throwable> causeMatcher) {
public static <T extends Throwable> Matcher<T> becauseOf(final Matcher<? extends Throwable> causeMatcher) {
return new TypeSafeMatcher<T>() {

@Override
Expand Down
62 changes: 52 additions & 10 deletions hamcrest/src/test/java/org/hamcrest/MatcherAssertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ public void describeMismatch(Object item, Description mismatchDescription) {
@Test public void
throwableIsOfMatchingInstance() {
assertThat(
() -> { throw new IllegalStateException(); },
new Executable() {
@Override
public void execute() {
throw new IllegalStateException();
}
},
throwsInstanceOf(IllegalStateException.class)
);
}
Expand All @@ -115,7 +120,12 @@ public void describeMismatch(Object item, Description mismatchDescription) {
+ " but: threw but <java.lang.IllegalStateException> is a java.lang.IllegalStateException";
try {
assertThat(
() -> { throw new IllegalStateException(); },
new Executable() {
@Override
public void execute() {
throw new IllegalStateException();
}
},
throwsInstanceOf(IOException.class)
);
fail("should have failed");
Expand All @@ -128,7 +138,12 @@ public void describeMismatch(Object item, Description mismatchDescription) {
@Test public void
throwableHasMatchingMessage() {
assertThat(
() -> { throw new Exception("message"); },
new Executable() {
@Override
public void execute() throws Exception {
throw new Exception("message");
}
},
doesThrow(withMessage(equalTo("message")))
);
}
Expand All @@ -140,7 +155,12 @@ public void describeMismatch(Object item, Description mismatchDescription) {
+ " but: threw but message was \"actual message\"";
try {
assertThat(
() -> { throw new Exception("actual message"); },
new Executable() {
@Override
public void execute() throws Exception {
throw new Exception("actual message");
}
},
doesThrow(withMessage("expected message"))
);
fail("should have failed");
Expand All @@ -157,7 +177,12 @@ public void describeMismatch(Object item, Description mismatchDescription) {
+ endLine + " but: did not throw";
try {
assertThat(
() -> {}, // Do nothing
new Executable() {
@Override
public void execute() {
// Do nothing
}
},
throwsInstanceOf(NoSuchMethodError.class)
);
fail("should have failed");
Expand All @@ -169,9 +194,15 @@ public void describeMismatch(Object item, Description mismatchDescription) {

@Test public void
throwableCauseMatches() {
Matcher<? extends Throwable> instanceOfMatcher = instanceOf(XMLStreamException.class);
assertThat(
() -> { throw new RuntimeException(new XMLStreamException()); },
doesThrow(becauseOf(instanceOf(XMLStreamException.class)))
new Executable() {
@Override
public void execute() {
throw new RuntimeException(new XMLStreamException());
}
},
doesThrow(becauseOf(instanceOfMatcher))
);
}

Expand All @@ -181,9 +212,15 @@ public void describeMismatch(Object item, Description mismatchDescription) {
String expectedMessage = endLine + "Expected: throws because of an instance of java.lang.NullPointerException"
+ endLine + " but: threw but cause <java.lang.IllegalArgumentException> is a java.lang.IllegalArgumentException";
try {
Matcher<? extends Throwable> instanceOfMatcher = instanceOf(NullPointerException.class);
assertThat(
() -> { throw new RuntimeException(new IllegalArgumentException()); },
doesThrow(becauseOf(instanceOf(NullPointerException.class)))
new Executable() {
@Override
public void execute() {
throw new RuntimeException(new IllegalArgumentException());
}
},
doesThrow(becauseOf(instanceOfMatcher))
);
fail("should have failed");
}
Expand All @@ -201,7 +238,12 @@ public void describeMismatch(Object item, Description mismatchDescription) {
try {
assertThat(
"Custom message",
() -> { throw new IllegalArgumentException(); },
new Executable() {
@Override
public void execute() {
throw new IllegalArgumentException();
}
},
throwsInstanceOf(NullPointerException.class)
);
fail("should have failed");
Expand Down

0 comments on commit 5058b28

Please sign in to comment.