Java code coverage ideas?

I am working on a Java project where I have an ant build that runs JUnit tests that are controlled by Cobertura. This works great, and we kept our lighting very high. For some classes, such as Hibernate objects, we have minimal code in them, but they have equals and hashCode methods. Testing these issues is a huge pain and draws a percentage of coverage. We tried to use the EqualsVerifier . Two classes have references to each other, which are often found in Hibernate objects.

We looked at using Commons EqualsBuilder, but then we lose the ability to generate self-generated IDE equals / hashCode methods. I know that EqualsBuilder can also be executed using reflection, but we don’t want to lose performance at runtime just to cover unit test time.

The ideal situation would be if we could tell Cobertura to simply ignore the equals and hashCode methods, but the patches there require us to annotate our classes, which seems a bit inconvenient.

So, I hope for ideas from others regarding what works well in such cases. Does anyone have any ideas on how to do this?

Thanks!

+4
source share
2 answers

It seems to me that you need to make a decision: either equals or hashCode are not important, in which case you should simply ignore the <100% code coverage indicator (or figure out how to ignore the method). Or they are important, and you should write unit tests to implement them. This may not be fun, but it doesn't seem like you care if these methods work correctly. In this case, you may have to test them.

+5
source

If this is not worth testing, you should probably not write code first.

In other words: if your equals and hashcode methods are used anywhere in your production code, you need it . So simple. Otherwise, it will cause errors. Believe me, it will be.

And by the way, “testing these is a huge pain” should never be enough reason to refuse testing. Huge pain usually translates as a big effort now, but is worth it in the end.

+1
source

All Articles