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()

  1. Belal Jafri says:

    If you have implemented a toString method, you have implemented it to serve a purpose. As you have said in this article, the purpose might be debugging or logging related. It is easy to think this does not warrant a proper test, the fact that you have tested for the existence of this method is good enough, but I strongly disagree. In my experience, the toString method is often disregarded and left unmaintained. This is not an issue until you sitting looking at logs from production and hoping desperately that the toString contained that crucial missing field.

    The problem is that toString methods are a pain to test, there isn’t a fixed format and if you have lots of fields, its near impossible. I personally use the ToStringVerifier library by JParams, it works really well, so maybe give it a try.

Leave a Reply

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