hardayal / hamcrest

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

Java Test Coverage problem #158

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
First: Great work. Thank you for this library which makes unit tests and 
especially failures much more readable.

Second: a minor problem in connection with test coverage analysis

When testing

  assertThat(object, not(equalTo(null)));

the corresponding method

  object.equals(null);

will not be called because of

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

where o2 is object and o1 is null.

This leads to incorrect data in test coverage reports because it appears that 
the corresponding part of the equals(Object o) with o==null method will never 
be reached.

Original issue reported on code.google.com by o...@off-topic.biz on 11 Aug 2011 at 7:38

GoogleCodeExporter commented 8 years ago
Should be a simple fix of

    return o2 == null || o2.equals(o1);

and probably a similar fix below for the reverse case.

Original comment by dharkn...@gmail.com on 30 Aug 2011 at 11:21

GoogleCodeExporter commented 8 years ago
I'm confused. You want to test that the equals() method on your object can 
handle null?

If so, should the test be 
  assertFalse(object.equals(null))
?

Original comment by smgfree...@gmail.com on 25 Apr 2012 at 11:20

GoogleCodeExporter commented 8 years ago

Original comment by t.denley on 12 May 2012 at 10:30

GoogleCodeExporter commented 8 years ago
I've been having a look at the Hamcrest code, and I can't see how you are 
getting this.  In fact, there is a unit test that proves the behaviour is not 
as you describe:

    public void testHonoursIsEqualImplementationEvenWithNullValues() {
        Object alwaysEqual = new Object() {
            @Override
            public boolean equals(Object obj) {
                return true;
            }
        };
        Object neverEqual = new Object() {
            @Override
            public boolean equals(Object obj) {
                return false;
            }
        };

        assertThat(alwaysEqual, equalTo(null));
        assertThat(neverEqual, not(equalTo(null)));
    }

I can only conclude, therefore, that you are using a different version of 
Hamcrest.  Please can you confirm which version you are using, and re-test your 
code against Hamcrest the 1.3RC2 jars.

Thanks,
-T

Original comment by t.denley on 20 May 2012 at 11:14

GoogleCodeExporter commented 8 years ago
closing, presumed fixed.

Original comment by t.denley on 18 Jul 2012 at 4:07