Unit Testing Java

  • strict warning: Non-static method view::load() should not be called statically in /hermes/walnaweb12a/b57/moo.greydragoncom/nodsw/sites/all/modules/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /hermes/walnaweb12a/b57/moo.greydragoncom/nodsw/sites/all/modules/views/handlers/views_handler_argument.inc on line 744.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /hermes/walnaweb12a/b57/moo.greydragoncom/nodsw/sites/all/modules/views/handlers/views_handler_filter.inc on line 607.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /hermes/walnaweb12a/b57/moo.greydragoncom/nodsw/sites/all/modules/views/handlers/views_handler_filter.inc on line 607.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /hermes/walnaweb12a/b57/moo.greydragoncom/nodsw/sites/all/modules/views/handlers/views_handler_filter_boolean_operator.inc on line 159.
Leeland's picture

Basic testing patterns are important. But it is also important to properly plan the testing. Building a matrix really helps make sure you are covering all the basis.

A simple case is something that will compare two items for you. These little helper classes need to be built all the time for sorted lists and other such uses. However, it is amazingly easy to get something out of alignment. So for argument sake lets stick with something simple: "Compare two strings as being equal, allowing nulls to be used such that 2 nulls return true."

So the comparison for these would be based off of this matrix:

Input 1 Input 2 Result
null null true
"A" null false
null "B" false
"A" "B" false
"A" different "A" true

Now you might think that was the end of it. However, now it is important to examine the implementation.

The question is should "A" == "A" be allowed? Maybe yes maybe no. Lets assume we want the answer to be no (we do not want to allow the same object to be considered equal to itself).

Therefore, we need to add one more test to the plan, specifically:

Input 1 Input 2 Result
"A" same object "A" false

Walking the table produces the following tests:

assertTrue(equalsAllowNull(null, null));
assertFalse(equalsAllowNull(null, "A"));
assertFalse(equalsAllowNull("A", null));
assertFalse(equalsAllowNull("A", "B"));
// new() to prevent compiler from reusing same instance...
assertTrue(equalsAllowNull("A", new String("A"))); 
// confirm compiler is not reusing the object "A" using .equals and not ==
assertFalse("A" == new String("A"));
assertTrue("A" == "A");
// confirm the function using .equals and not ==
assertFalse(equalsAllowNull("A", "A"));

Which will all test the the actual function:

public boolean equalAllowNull(String a, String b) {

  return a == null ? b == null : a == b ? false : a.equals(b);

}

Thread Slivers eBook at Amazon