You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ofbiz.apache.org by Adam Heath <do...@brainfood.com> on 2010/02/17 22:15:47 UTC

java-based test case best practices

It is generally good to have a test suite per class that is being
tested.  Then, a single test method for each method in the orignal
class.  All code blocks need to be tested, including error conditions.
  Sometimes this might entail writing support classes to inject
errors, and cause exceptions to be thrown(like I have done in the
UtilObjectTests class).

Do not make use of any internal objects or interfaces.  For instance,
if a method is currently creating a new HashMap directly, then
populating it, don't test the return value as being a HashMap; the
method might switch to another map implementation in the future.

The names of the test method should generally be
test{ProperCaseMethodName}.  If there are multiple overridden variants
for a method(different parameter types), then a suffix should be
added, with each parameter type joined together with '_'.  Ie,
testGetBytes_Object, or testGetBytes_Stream.

If some code being tested has a throws clause, and the test itself is
not supposed to throw an exception unless something is wrong, then add
a throws Exception to the test case method.  If you are testing that a
correct exception is thrown, then you should use a try/catch/finally
triplet to do so(look at UtilObjectTests for examples).