liangzai-cool / hamcrest

Automatically exported from code.google.com/p/hamcrest
0 stars 0 forks source link

IsEqual matcher checks for null which result in less code coverage for the class under test. #37

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I was testing one of my class, specifically the equals(Object) method, and
could not get full coverage for the use case where Object is null.
this is because the isEqual matcher use areEqual(Object, Object) as
follows: (here is the code)
 private static boolean areEqual(Object o1, Object o2) {
        if (o1 == null || o2 == null) {
            return o1 == null && o2 == null;
        } else if (isArray(o1)) {
            return isArray(o2) && areArraysEqual(o1, o2);
        } else {
            return o1.equals(o2);
        }
    }

I understand why we need to check  if o1 equals null, but checking both of
them is just reimplementing the equals(Object) I would like to test.

So I went ahead and fixed my local source code of IsEqual to look like this:

private static boolean areEqual(Object o1, Object o2) {
  if (o1 == null)
        return false;

   if (isArray(o1)) 
        return isArray(o2) && areArraysEqual(o1, o2);

   return o1.equals(o2);
}

and now I get 100% coverage for my code.

Original issue reported on code.google.com by goren.ve...@gmail.com on 10 Jun 2008 at 7:10

GoogleCodeExporter commented 9 years ago
The IsEqual matcher assumes a correct implementation of equals(o).  It cannot 
be used
to test an implementation of equals.

To test equals using assertThat, compare the result to a boolean value.  E.g.

  assertThat(o.equals(null), equalTo(false));
  assertThat(o.equals(o), equalTo(true));

  ... etc.

Original comment by nat.pr...@gmail.com on 10 Jun 2008 at 9:53

GoogleCodeExporter commented 9 years ago
Duplicate of issue 22, which is fixed in trunk.

Please search the existing issues to avoid submitting duplicates.

Original comment by nat.pr...@gmail.com on 10 Jun 2008 at 9:55