From 5058b286eef8b00d0c7cebd6f38f04b0e6a4f755 Mon Sep 17 00:00:00 2001 From: Peter De Maeyer Date: Sun, 21 Feb 2021 10:43:14 +0100 Subject: [PATCH] #329 Updated fix for Java 1.7 --- .../main/java/org/hamcrest/Executable.java | 1 - .../src/main/java/org/hamcrest/Throws.java | 6 +- .../java/org/hamcrest/MatcherAssertTest.java | 62 ++++++++++++++++--- 3 files changed, 55 insertions(+), 14 deletions(-) diff --git a/hamcrest/src/main/java/org/hamcrest/Executable.java b/hamcrest/src/main/java/org/hamcrest/Executable.java index fb964219..6dace114 100644 --- a/hamcrest/src/main/java/org/hamcrest/Executable.java +++ b/hamcrest/src/main/java/org/hamcrest/Executable.java @@ -1,6 +1,5 @@ package org.hamcrest; -@FunctionalInterface public interface Executable { void execute() throws Throwable; diff --git a/hamcrest/src/main/java/org/hamcrest/Throws.java b/hamcrest/src/main/java/org/hamcrest/Throws.java index fa64b9eb..f4fc94a5 100644 --- a/hamcrest/src/main/java/org/hamcrest/Throws.java +++ b/hamcrest/src/main/java/org/hamcrest/Throws.java @@ -10,7 +10,7 @@ public final class Throws implements SelfDescribing { private final Matcher matcher; - public Throws(Matcher throwableMatcher) { + public Throws(final Matcher throwableMatcher) { requireNonNull(throwableMatcher); this.matcher = new BaseMatcher() { @@ -46,7 +46,7 @@ public void describeMismatch(Description description) { description.appendText("did not throw"); } - public static Matcher withMessage(Matcher messageMatcher) { + public static Matcher withMessage(final Matcher messageMatcher) { return new TypeSafeMatcher() { @Override @@ -68,7 +68,7 @@ protected void describeMismatchSafely(T item, Description mismatchDescription) { }; } - public static Matcher becauseOf(Matcher causeMatcher) { + public static Matcher becauseOf(final Matcher causeMatcher) { return new TypeSafeMatcher() { @Override diff --git a/hamcrest/src/test/java/org/hamcrest/MatcherAssertTest.java b/hamcrest/src/test/java/org/hamcrest/MatcherAssertTest.java index 3110d3fb..a56f58f8 100644 --- a/hamcrest/src/test/java/org/hamcrest/MatcherAssertTest.java +++ b/hamcrest/src/test/java/org/hamcrest/MatcherAssertTest.java @@ -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) ); } @@ -115,7 +120,12 @@ public void describeMismatch(Object item, Description mismatchDescription) { + " but: threw but 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"); @@ -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"))) ); } @@ -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"); @@ -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"); @@ -169,9 +194,15 @@ public void describeMismatch(Object item, Description mismatchDescription) { @Test public void throwableCauseMatches() { + Matcher 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)) ); } @@ -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 is a java.lang.IllegalArgumentException"; try { + Matcher 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"); } @@ -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");