JavaHamcrest icon indicating copy to clipboard operation
JavaHamcrest copied to clipboard

Implement matchers for thrown exceptions in `Runnable`

Open ggalmazor opened this issue 4 months ago • 2 comments

Fixes #329

This PR is inspired by @peterdemaeyer's #330, which fell through the cracks two years ago. I want to rekindle this effort to have matchers for exceptions thrown in Runnable.

The goal of this matcher is to provide an easy way to verify that some code will throw some expected exception by wrapping it inside a Runnable that's executed safely by the matcher and tests the thrown exception with the provided matchers, as in:

assertThat(() -> SomeCode.thatThrows(), throwsException(withMessage("Boom!")));

This is my first contribution to this repo, so I expect my changes won't align with current conventions. I've tried to imitate patterns I've seen in other TypeSafeMatcher subclasses, but I can't be sure if the result is 100% acceptable. Please point me in the right direction, and I'll apply any changes needed to merge this PR.

In this PR:

  • New throwsException type-safe matcher for Runnable
  • New throwsExceptionEqualTo type-safe matcher for Throwable
  • New throwsExceptionWithMessage type-safe matcher for Throwable

ggalmazor avatar Oct 06 '24 20:10 ggalmazor