ncredinburgh / JavaForSmallTeams

Guidance on writing "good" server side java
https://www.gitbook.com/book/ncrcoe/java-for-small-teams
Other
69 stars 31 forks source link

Tested Expected Exceptions #27

Open grantjforrester opened 8 years ago

grantjforrester commented 8 years ago

I wondered if you'd seen: https://github.com/Codearte/catch-exception

I particularly like the fluent style for asserting features of the thrown exception. (from the README) ..

// given: an empty list
List myList = new ArrayList();

// when: we try to get the first element of the list
when(myList).get(1);

// then: we expect an IndexOutOfBoundsException
then(caughtException())
        .isInstanceOf(IndexOutOfBoundsException.class)
        .hasMessage("Index: 1, Size: 0")
        .hasNoCause();

I always recommend some testing of the message to ensure your failing for the right reason. I've seen countless examples where the exception type was valid but the reason was not what was expected.