Closed sebiniemann closed 5 years ago
A possibility would be to test the exception using the ExpectedException rule, so we have the opportunity to check the exception texts, too. To solve this as efficient as possible, a split into separate classes would be appropriate. Example for an exception test with exception-message test:
@Rule public ExpectedException thrown = ExpectedException.none();
@Test public void shouldTestExceptionMessage() throws IndexOutOfBoundsException { List
thrown.expect(IndexOutOfBoundsException.class);
thrown.expectMessage("Index: 0, Size: 0");
list.get(0); // execution will never get past this line
}
This sounds just right, but we should also group the exception messages to improve on future test maintenance.
For example instead of writing ...
...
thrown.expectMessage("Index: 0, Size: 0");
...
thrown.expectMessage("Index: 0, Size: 0");
...
thrown.expectMessage("Index: 0, Size: 0");
...
... we could generate the string just once (maybe with placeholder within?) and reuse it later on:
String exceptionMessage = "Index: 0, Size: 0";
...
thrown.expectMessage(exceptionMessage);
...
thrown.expectMessage(exceptionMessage);
...
thrown.expectMessage(exceptionMessage);
...
We should decide on a best practice to perform test on exception handling, before further development in this field.