Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement matchers for thrown exceptions in Runnable #423

Merged
merged 16 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Change: add composable exception message matcher
This matcher doesn't care about the exception's class
  • Loading branch information
ggalmazor committed Oct 27, 2024
commit b6d7137b2d0bcbd484e5355eadc5b9f92a5d3936
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.hamcrest.exception;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

import static org.hamcrest.core.IsEqual.equalTo;

/**
* A matcher that checks if a Runnable throws an expected exception when run.
*
* @param <T> the type of the expected exception.
*/
public class ThrowsExceptionWithMessage<T extends String> extends TypeSafeMatcher<Runnable> {
private final Matcher<? super T> messageMatcher;
private String actualMessage;

/**
* Constructor, best called from one of the static factory methods.
*
* @param messageMatcher matches the exception message
*/
ThrowsExceptionWithMessage(Matcher<? super T> messageMatcher) {
super(Runnable.class);
this.messageMatcher = messageMatcher;
}

public static <U extends String> ThrowsExceptionWithMessage<U> withMessage(U message) {
return new ThrowsExceptionWithMessage<>(equalTo(message));
}

public static <U extends String> ThrowsExceptionWithMessage<U> withMessage(Matcher<? super U> messageMatcher) {
return new ThrowsExceptionWithMessage<>(messageMatcher);
}

@Override
protected boolean matchesSafely(Runnable item) {
try {
item.run();
return false;
} catch (Throwable t) {
actualMessage = t.getMessage();
return this.messageMatcher.matches(t.getMessage());
}
}

@Override
public void describeTo(Description description) {
description.appendText("a runnable throwing an exception with message ").appendDescriptionOf(messageMatcher);
}

@Override
protected void describeMismatchSafely(Runnable item, Description mismatchDescription) {
mismatchDescription.appendText("exception message was ").appendValue(actualMessage).appendText(" instead of ").appendDescriptionOf(messageMatcher);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import org.hamcrest.AbstractMatcherTest;
import org.hamcrest.Matcher;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.exception.ThrowsException.throwsException;
import static org.hamcrest.exception.ThrowsExceptionWithMessage.withMessage;

public class ThrowsExceptionEqualTest extends AbstractMatcherTest {

Expand Down Expand Up @@ -36,6 +38,18 @@ public void testEvaluatesToTrueIfRunnableThrowsExceptionExtendingTheExpectedExce
assertMatches(matcher, runnableThrowing(new TestException("Boom!")));
}

public void testEvaluatesToTrueIfRunnableThrowsExceptionWithExpectedMessage() {
Matcher<Runnable> matcher = throwsException(withMessage("Boom!"));

assertMatches(matcher, runnableThrowing(new TestException("Boom!")));
}

public void testEvaluatesToTrueIfRunnableThrowsExceptionWithMatchingMessage() {
Matcher<Runnable> matcher = throwsException(withMessage(containsString("bar")));

assertMatches(matcher, runnableThrowing(new TestException("Foo bar baz")));
}

public static class TestException extends IllegalArgumentException {
public TestException(String message) {
super(message);
Expand Down