Transilvania JUG



How to test for the implementation of toString()

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:

  1. – 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.

  2. – 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.

  3. – 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

Standard postscript: this is a guest-post by Attila Balazs (aka Cd-MaN). The text of the article is available under the CC-BY-SA license. The associated source code is available under public domain, CC0 or the MIT/X License, depending on your preference.

One Response to How to test for the implementation of toString()

Leave a Reply

Your email address will not be published. Required fields are marked *