How can I exclude a methods (hashcode and equals) from the clover coverage report?
Asked Answered
S

2

6

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

Scampi answered 3/1, 2012 at 10:51 Comment(3)
Why wouldn't you want to test those methods? I think it's a mistake.Ballistics
did you figured out any solutions for this? would be really nice if you could shareWoad
no.... still no solution, actually I've ignored this problem for now....Scampi
R
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:

Resurrectionist answered 5/11, 2013 at 21:56 Comment(0)
F
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.

Festination answered 3/1, 2012 at 14:8 Comment(3)
I did not ask whenever it makes sense to test equals/hashcode, or how to do it, but how to exclude them from cloverScampi
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.Festination
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...)Unreflective
R
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:

Resurrectionist answered 5/11, 2013 at 21:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.