junit-team/junit — pull request #801 by oskadsem (2014-01-13 07:24:00+00:00)
Title: Testing contents of exception-messages

I've more than once wanted to test at least some part of the exception messages our applications throw. We accept input from various sources and have standards for validation and the level of detail of the exception messages that should be thrown for bad input. So we quite often end up with code like this:

```
public void method(final Long id, final String parameter) {
Validate.notNull(id, "id cannot be null");
Validate.notEmpty(parameter, "parameter cannot be an empty string (id=" + id + ")");
// ...
}
```

We've used @Rule and ExpectedException to test these validations separately so far, and it works ok. But it does sort of feel like a few of lines more than necessary, and we have lot of these types of situations... So this is an attempt to make it slightly easier to test these types of situations, by allowing for constructs like this:

```
@Test(expectedMessage = "id cannot be null")
public void shouldThrowExceptionForMissingId() {
service.method(null, "Something");
}

@Test(expectedMessage = "parameter cannot be empty")
public void shouldThrowExceptionForMissingParameter() {
service.method(1L, null);
}
```

Note:
I've tried following code standards and placed logic where it seemed appropriate, but I'm open to any and all criticism. I also hope this is the correct way of trying to make a contribution, I had to do some reading on how Github works ;).