sebiniemann / ArmadilloJava

Pure Java-based linear algebra library
armadillojava.org
8 stars 8 forks source link

First experience with exception testing #105

Closed sebiniemann closed 5 years ago

sebiniemann commented 10 years ago

We should decide on a best practice to perform test on exception handling, before further development in this field.

dkiechle commented 10 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 list = new ArrayList();

    thrown.expect(IndexOutOfBoundsException.class);
    thrown.expectMessage("Index: 0, Size: 0");
    list.get(0); // execution will never get past this line
}
sebiniemann commented 10 years ago

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);
...