Skip to content

Commit

Permalink
Add throwsException matcher for throwable instance
Browse files Browse the repository at this point in the history
  • Loading branch information
tumbarumba committed Nov 3, 2024
1 parent 544a4cf commit f0545a5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions hamcrest/src/main/java/org/hamcrest/Matchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -2240,6 +2240,18 @@ public static <T extends Runnable> Matcher<T> throwsException() {
return ThrowsException.throwsException();
}

/**
* Matcher for {@link Throwable} that expects that the Runnable throws an exception equal to the provided <code>throwable</code>
*
* @param <U> type of the Runnable
* @param <T> type of the Throwable
* @param throwable the Throwable class against which examined exceptions are compared
* @return The matcher.
*/
public static <T extends Runnable, U extends Throwable> Matcher<T> throwsException(U throwable) {
return ThrowsException.throwsException(throwable);
}

/**
* Matcher for {@link Throwable} that expects that the Runnable throws an exception of the provided <code>throwableClass</code> class
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public static <T extends Runnable> Matcher<T> throwsException() {
return throwsException(Throwable.class);
}

public static <T extends Runnable, U extends Throwable> Matcher<T> throwsException(U throwable) {
return throwsException(throwable.getClass(), throwable.getMessage());
}

public static <T extends Runnable, U extends Throwable> Matcher<T> throwsException(Class<U> throwableClass) {
return new ThrowsException<>(new IsInstanceOf(throwableClass), anything("<anything>"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Matchers of exceptions.
*/
package org.hamcrest.exception;
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.throwsExceptionWithMessage;
import static org.hamcrest.exception.ThrowsException.throwsException;
import static org.hamcrest.test.MatcherAssertions.*;

Expand All @@ -22,7 +23,10 @@ public void examples() {
assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException());
assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException(RuntimeException.class));
assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException(RuntimeException.class, "Boom!"));
assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException(new IllegalArgumentException("Boom!")));
assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsException(RuntimeException.class, containsString("Boo")));
assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsExceptionWithMessage("Boom!"));
assertThat(ThrowsExceptionTest::throwIllegalArgumentException, throwsExceptionWithMessage(containsString("Boo")));
}

@Test
Expand Down

0 comments on commit f0545a5

Please sign in to comment.