我想知道目前是否可以忽略 Sonar 测试覆盖的 equals 和 hashcode 方法?我听说过 block 排除,但它没有用。
最佳答案
(假设您使用 jacoco 进行覆盖率报告)
如果您不使用 Lombok,您可以尝试将 @Generated 注释添加到您想要跳过的方法中。我不确定这是否有效——但值得一试!
如果您[像我一样]使用 Lombok,这里有一个来自 Rainer Hahnekamp 的解决方案,它将代码标记为@Generated,这使得 jacoco 忽略这些方法,进而使 sonarqube 显示更高的覆盖率。
Luckily, beginning with version 0.8.0, Jacoco can detect, identify, and ignore Lombok-generated code. The only thing you as the developer have to do is to create a file named lombok.config in your directory’s root and set the following flag:
lombok.addLombokGeneratedAnnotation = true
This adds the annotation lombok.@Generated to the relevant methods, classes and fields. Jacoco is aware of this annotation and will ignore that annotated code.
Please keep in mind that you require at least version 0.8.0 of Jacoco and v1.16.14 of Lombok.
https://www.rainerhahnekamp.com/en/ignoring-lombok-code-in-jacoco/
关于sonarqube - 如何让sonarqube忽略等号和哈希码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47923297/