Update: This entry has been crossposted to the transylvania-jug blog.
Problem statement: you have some value objects for which you implemented toString() (for debugging purposes) and now you would like to test using a unit test that these implementations exist.
Possible solutions:
- Use reflection to detect the existence of the method:
boolean hasToStringViaReflection(Class> clazz) { Method toString; try { toString = clazz.getDeclaredMethod("toString"); } catch (NoSuchMethodException ex) { return false; } if (!String.class.equals(toString.getReturnType())) { return false; } return true; }
Advantage: no third party libraries needed. Also, no instance of the class is needed. Disadvantage: the actual code is not executed, so even trivial errors (like null dereferences) are not caught. Also, code coverage tools will report the lines as not covered.
- Compare the string returned by the toString method to the string returned by Object and expect them to be different. This uses ObjectUtils from Apache Commons Lang:
boolean hasToStringViaInvocation(Object o) { return !ObjectUtils.identityToString(o).equals(o.toString()); }
Advantage: the actual code is executed, so trivial errors are detected. Also the code will be “covered”. Disadvantage: it requires an external library (however Commons Lang contains a lot of goodies, so it is sensible to add it most of the time). Also, it requires an instance of the class, so you need to be able to instantiate it.
- Don’t use hand-coded methods at all, but rather some code-generation / AOP style programming like Project Lombok.
Again, these methods are to be used for toString methods which have debugging purpose only. In case the output of the method needs to conform to some stricter rule, more checks need to applied.
The complete source code can be found on Google Code under Public Domain or the BSD license.