eclipse 提高 Lombok @Data 代码覆盖率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44584487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:26:09  来源:igfitidea点击:

Improve Lombok @Data Code Coverage

javaeclipseunit-testingcode-coverageeclemma

提问by Varun Sharma

I am writing unit tests for my project and am trying to achieve at least 80% code coverage. Problem is that I am using lombok's @Dataannotation for generating getters and setters and when I run my unit tests, all those getters and setters along with other methods like toString, equals, hashcodeetc are missed and my code coverage takes a hit. Is there any workaround for this. I have been searching a lot about this but haven't been able to find anything which could help out. Any help on this would be appreciated.

我正在为我的项目编写单元测试,并试图实现至少 80% 的代码覆盖率。问题是,我使用的龙目岛的@Data注解产生getter和setter,当我跑我的单元测试,所有的getter和setter方法以及其他方法,如toStringequalshashcode等错过,我的代码覆盖需要一个打击。是否有任何解决方法。我一直在对此进行很多搜索,但找不到任何可以提供帮助的信息。对此的任何帮助将不胜感激。

I am using Eclemma for code coverage analysis.

我正在使用 Eclemma 进行代码覆盖率分析。

采纳答案by ahmetcetin

First of all, @Dataannotation is the combination of @ToString, @EqualsAndHashCode, @Getter, @Setter.

首先,@Data注释是组合 @ToString@EqualsAndHashCode@Getter@Setter

If you just need Lombok to create getters and setters automatically, you can use only @Getterand @Setterannotations instead of @Data.

如果您只需要 Lombok 自动创建 getter 和 setter,则只能使用@Getter@Setter批注而不是@Data

Besides, to keep the methods created by Lombok outside of this coverage, you can create a lombok.configfile in your root directory and have these two lines:

此外,为了将 Lombok 创建的方法保留在此范围之外,您可以在根目录中创建一个lombok.config文件并包含以下两行:

config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true

After adding this line, when you go to Sonar, you will see that these classes are covered 100%.

添加此行后,当您转到Sonar 时,您会看到这些类被100%覆盖。

回答by mladzo

In 0.8.0 release, Jacoco added support for filtering out all methods annotated with @lombok.Generatedfrom their reports. The only thing you need to change is to add lombok.configto the root of your project with the following settings:

0.8.0 版本中,Jacoco 添加了对@lombok.Generated从其报告中过滤掉所有注释的方法的支持。您唯一需要更改的是lombok.config使用以下设置添加到项目的根目录:

config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
  • config.stopBubbling = truetells Lombok that this is your root directory and that it should not search parent directories for more configuration files (you can have more than one lombok config files in different directories/packages).
  • lombok.addLombokGeneratedAnnotation = truewill add @lombok.Generated annotation to all Lombok generated methods.
  • config.stopBubbling = true告诉 Lombok 这是你的根目录,它不应该在父目录中搜索更多的配置文件(你可以在不同的目录/包中有多个 lombok 配置文件)。
  • lombok.addLombokGeneratedAnnotation = true将 @lombok.Generated 注释添加到所有 Lombok 生成的方法。

And that's it. Jacoco will filter Lombok auto-generated methods, and if you give your best, your code coverage could be close to 100% :))

就是这样。Jacoco 将过滤 Lombok 自动生成的方法,如果您尽力而为,您的代码覆盖率可能接近 100% :))

回答by Chris K

When equals and hashcode are needed, they can be unit tested very thouroughly by using EqualsVerifier. EqualsVerifier is an opensource JUnit library that generates the unit tests for all parts of the equals and hashCode contracts, something that is not straight forward to achieve even when writing the tests by hand.

当需要 equals 和 hashcode 时,可以使用EqualsVerifier对它们进行非常彻底的单元测试。EqualsVerifier 是一个开源 JUnit 库,它为 equals 和 hashCode 合约的所有部分生成单元测试,即使手动编写测试也无法直接实现。

Example usage:

用法示例:

@Test
public void equalsContract() {
    EqualsVerifier.forClass( MyAwesomeLombokedDataClass.class )
        .suppress( Warning.STRICT_INHERITANCE )
        .verify();
}