R
Rhino
I'm having a bit of a conceptual struggle right now with the right way to
handle exceptions in JUnit tests. Since I always explain concepts better in
terms of examples, let me describe a scenario to you which should
illustrate the situation clearly.
I have a method called getRGBColor() whose input parameter is a six letter
String that is supposed to contain exactly six hex digits. Those hex digits
are the String representation of an RGB Color. Therefore, "FFFFFF"
represents White.
I have no trouble writing a JUnit test that passes in "FFFFFF" as an input
value and then verifies that the Color returned by the method is White i.e.
Color(255,255,255) so I can test that aspect of the code without trouble.
However, the method throws IllegalArgumentException if the input String is
not precisely 6 characters long or if any of the characters are not hex
digits.
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. Now, here's where my problem comes in. When I execute this
test, the method in which it is situated inevitably results in a black X
decoration on the test result in the JUnit window of Eclipse. (I'm using
JUnit3 for now.) I know that's reasonable given that it is correctly
reporting an exception thrown by a method that is under test.
However, I had the distinct impression in a conversion some months ago,
that a properly written set of JUnit tests should always give me a green
checkmark decoration on every test. And that makes a lot of sense to me
too. Ideally, given that unit tests may be executed hundreds of times and
each test class may have dozens of test methods in it, the last thing
anyone wants to do is have to inspect each test method individually and
know exactly which ones should have had green checkmarks, black X's and red
X's. It's simply far easier to make sure that all of the tests had green
checkmarks and only dive into them if some test _doesn't_ have a green
checkmark.
I don't see how I can reconcile these two ideas. If I test for thrown
exceptions, I will inevitably get some black X's. (Unless there is some way
to negate the exception test so that it only gives the black X if the
exception FAILS to be thrown???) But if I get black X's, how am I to know
quickly which of the test methods SHOULD be getting black X's and which
ones should be getting green checkmarks? I don't much like the idea of
trying to document all that and I certainly can't memorize it all....
handle exceptions in JUnit tests. Since I always explain concepts better in
terms of examples, let me describe a scenario to you which should
illustrate the situation clearly.
I have a method called getRGBColor() whose input parameter is a six letter
String that is supposed to contain exactly six hex digits. Those hex digits
are the String representation of an RGB Color. Therefore, "FFFFFF"
represents White.
I have no trouble writing a JUnit test that passes in "FFFFFF" as an input
value and then verifies that the Color returned by the method is White i.e.
Color(255,255,255) so I can test that aspect of the code without trouble.
However, the method throws IllegalArgumentException if the input String is
not precisely 6 characters long or if any of the characters are not hex
digits.
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. Now, here's where my problem comes in. When I execute this
test, the method in which it is situated inevitably results in a black X
decoration on the test result in the JUnit window of Eclipse. (I'm using
JUnit3 for now.) I know that's reasonable given that it is correctly
reporting an exception thrown by a method that is under test.
However, I had the distinct impression in a conversion some months ago,
that a properly written set of JUnit tests should always give me a green
checkmark decoration on every test. And that makes a lot of sense to me
too. Ideally, given that unit tests may be executed hundreds of times and
each test class may have dozens of test methods in it, the last thing
anyone wants to do is have to inspect each test method individually and
know exactly which ones should have had green checkmarks, black X's and red
X's. It's simply far easier to make sure that all of the tests had green
checkmarks and only dive into them if some test _doesn't_ have a green
checkmark.
I don't see how I can reconcile these two ideas. If I test for thrown
exceptions, I will inevitably get some black X's. (Unless there is some way
to negate the exception test so that it only gives the black X if the
exception FAILS to be thrown???) But if I get black X's, how am I to know
quickly which of the test methods SHOULD be getting black X's and which
ones should be getting green checkmarks? I don't much like the idea of
trying to document all that and I certainly can't memorize it all....