This style of code can be expensive at runtime:

   for (int i = 0; i < count; i++) {
      assertEquals("Testing at iteration " + i, f(i), g(i));
   }

The issue is the message will be created 'count' times even though it is never used. This takes up cpu time and garbage collection time.

In the Derby sanity code this style was a problem, causing extended test running time, so the encouraged practice (for Derby sanity checks) is

  if (SanityManager.ASSERT)
  {
     if (X != Y)
        SanityManager.THROWASSERT("Value should be " + X + " is " + Y);
  }

rather than

  if (SanityManager.ASSERT)
  {
     SanityManager.ASSERT(X == Y, "Value should be " + X + " is " + Y);
  }

We're still deciding what the best practice for JUnit tests should be, but based upon past experience when we see a loop with a complex iteration specific assert message we worry about execution time.

In some cases the message can be fixed, e.g. this coding style should not be encouraged:

  assertEquals("Expected " + X + " to be the equal to " + Y, X, Y);

This is because the JUnit mechanism will print out the value of X and Y, so no need to repeat it in a generated message.

  • No labels