Re: Concept question about JUnit Failures
On 18-05-2010 17:03, Tom Anderson wrote:
On Tue, 18 May 2010, Rhino wrote:
I can easily invoke the method with a value that will cause the
exception to be thrown and I can catch it with a standard try/catch
block. I can also put code in my catch block to make sure that JUnit
reports the error from the Exception in the method being tested. I end
up with this test which correctly reports the IllegalArgumentException
within the JUnit window:
try {
Color actualRGBColor = colorConversionUtils.getRGBColor("FFFFFFA");
Color expectedRGBColor = new Color(255, 255, 255);
assertTrue("Actual color, " + actualRGBColor + ", does not equal expected
color, " + expectedRGBColor, actualRGBColor.equals(expectedRGBColor));
}
catch (IllegalArgumentException ia_excp) {
assertionFailedError = new AssertionFailedError(ia_excp.getMessage());
assertionFailedError.initCause(ia_excp);
throw assertionFailedError;
}
So far, so good.
No, not good, this is completely wrong.
You want this:
@Test(expected=IllegalArgumentException.class)
public void getRGBColorThrowsExceptionOnBadInput() {
colorConversionUtils.getRGBColor("FFFFFFA");
}
If he can upgrade from JUnit 3 to 4.
Otherwise the fail method as show by Daniel Pitt must be the
solution.
JUnit 4 is relative old now, so I can not see any reasons not
to upgrade (at least not for a project where the work to convert
is not a problem in itself).
Arne