Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

clover

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

like image 308
Maciej Miklas Avatar asked Oct 09 '22 02:10

Maciej Miklas


2 Answers

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.

like image 104
Gray Avatar answered Oct 19 '22 22:10

Gray


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:

  • https://confluence.atlassian.com/display/CLOVER/Using+Coverage+Contexts
like image 38
Marek Avatar answered Oct 19 '22 22:10

Marek