5

I would like to exclude hashCode and equals from clover report.
Some configuration example would be nice.

oers
  • 18,436
  • 13
  • 66
  • 75
Maciej Miklas
  • 3,305
  • 4
  • 27
  • 52

2 Answers2

3

I would like to exclude hashCode and equals from clover report.

I would respectfully suggest that you actually test these methods instead of avoiding them. Serious bugs can occur if they are not consistent with specifications. I've encountered NPEs and other problems in poorly written hashCode and equals methods as well. Here's a great link with a number of ways that you can test your methods:

How should one unit test the hashCode-equals contract?

We use the following LocalEqualsHashCodeTest which can be extended by a unit test:

http://pastebin.com/L03fHAjv

You then define a createInstance() method which returns an instance of your class and a createNotEqualInstance() method which returns another instance that is not equal to the first one.

Community
  • 1
  • 1
Gray
  • 115,027
  • 24
  • 293
  • 354
  • 3
    I did not ask whenever it makes sense to test equals/hashcode, or how to do it, but how to exclude them from clover – Maciej Miklas Dec 06 '12 at 09:37
  • 4
    That's fine @MaciejMiklas. I just wanted to give you a different way of looking at things. Tons of answers on SO don't address the specific question but encourage the poster to look it differently. The fact is that I don't know but I consider it a bad practice to not test them. – Gray Dec 06 '12 at 13:10
  • 3
    Funny, I came here looking for a way to exclude from my cobertura site report hashcode and equals, but actually your way (testing them with that utility class) seems much more better. So thanks! (btw, i dont understand why OP was so harsh about your answer...) – juancancela May 12 '14 at 02:04
1

You have to do two steps:

1) Define method contexts in the <clover-setup> task containing regular expressions for methods you want to match, for example:

<clover-setup ...>
    <methodContext name="equals" regexp="public boolean equals\(.*\)"/>
    <methodContext name="hashCode" regexp="public int hashCode\(\)"/>
</clover-setup>

2) Define which method contexts shall be excluded from the report in the <clover-report> task

<clover-report>
   <current outfile="clover_html" title="My Coverage">
     <format type="html" filter="equals,hashCode"/>
   </current>

More information:

Marek
  • 748
  • 6
  • 13