RichardWarburton/lambda-behave

Add possibility to match message of expected Exception

Opened this issue · 3 comments

Currently when we expect some exception we can just check class of expected Exception. It would be useful if some matcher could match on exception's message (maybe event fields of Exception).

...how do I use this in the case that the method I'm calling throws and exception. Obviously in this case it has to be wrapped in a try/catch in the test body. Here's an example I've come up with.

it.should("throw an error when you ask for a coffee type that doesn't exist.", expect -> {
               expect.exception(NoSuchCoffeeException.class, () -> {
                   coffeeMaker.makeCoffee("welsh", false, 37);
               });
       });

...method signature....
public Coffee makeCoffee(String type, boolean milk, int sugars) throws NoSuchCoffeeException {

If you look at the linked git commit there's an example of the specification:

6346bb8#diff-d51eaef31041834df2e1c4a9f425e4b8

...problem was that my exception was extending Throwable rather than Exception, so IDE was complaining... Thank you for your help :)