I have something like
@Test(expected = IllegalArgumentException.class)
public void cdIntoNonExistantFolder() {
cdTool.changeDirectory("nonexistant");
assertThat(cdTool.getStatusCode(), not(equalTo(0)));
}
I believe the assertThat
does not run as changeDirectory
will throw the exception. Is it possible to make it still run?
You could use a finally
:
@Test(expected = IllegalArgumentException.class)
public void cdIntoNonExistantFolder() {
try {
cdTool.changeDirectory("nonexistant");
}
finally {
assertThat(cdTool.getStatusCode(), not(equalTo(0)));
}
}
I'd rather avoid any try/catch structures in unit-tests. Here's one possibility with Catch-Exception:
@Test
public void cdIntoNonExistantFolder() {
catchException(cdTool).changeDirectory("nonexistant");
assertThat(caughtException(), instanceOf(IllegalArgumentException.class));
assertThat(cdTool.getStatusCode(), not(equalTo(0)));
}
or with JUnit 5:
@Test
public void cdIntoNonExistantFolder() {
expectThrows(IllegalArgumentException.class, () -> {
cdTool.changeDirectory("nonexistant");
});
assertThat(cdTool.getStatusCode(), not(equalTo(0)));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With