My Test: this is where it underlines the stuff after sax. and insists that I have a try-catch block…. but the internet says that the proper way to test for exception is @Test(expected=IllegalArgumentException.class)
@Test(expected= XMLClientNotFoind.class)
public void testGetClientFromIP() throws XMLClientNotFound{
...
assertEquals(c, sax.getClientFromIP("101.0.2.01"));
}
And the method, getClientFromIP is here:
public Client getClientFromIP(String ip) throws XMLClientNotFound {
...
throw new XMLClientNotFound();
}
And my exception:
public class XMLClientNotFound extends Exception{
}
First of all:
should not be considered as a proper way, especially with such a generic exception. The reason is that you have no control over which statement in your test method actually threw the exception. Also you can’t make any assertions on the message label, cause, etc.
Using
try-catchprecisely surrounding a line that is suppose to throw an exception is the right way to go:You might also try JUnit
@Rulethat I wrote some time ago to make your test more readable.