简体   繁体   English

排除 Lombok @Builder 附带的 toString() 方法

[英]Exclude toString() method that comes with Lombok @Builder

According to this Lombok documentation , there are 7 things generated by annotating a class with @Builder , one of them being " A sensible toString() implementation "根据这个Lombok 文档,通过使用@Builder注释类生成了 7 个东西,其中之一是“一个合理的 toString() 实现

In the project I am working on, this default toString() method has in fact caused some annoyance that jacoco report considers the method was not tested, and indeed it wasn't, because I did not mean to have a toString() method at all.在我正在做的项目中,这个默认的toString()方法实际上引起了一些烦恼, jacoco 报告认为该方法没有经过测试,事实上它没有,因为我不是故意在那里有一个toString()方法全部。

Is there a way to avoid the generation of the default toString() method, and if so, how?有没有办法避免生成默认的toString()方法,如果有,如何避免?

Any thoughts or insight would be much appreciated.任何想法或见解将不胜感激。

I don't think you can remove toString(), but you can always just test it and validate that there is no nullPointerException throne.我不认为您可以删除 toString(),但您始终可以对其进行测试并验证是否没有 nullPointerException 宝座。

If you have np methods in your class, then it would be better just add this class as an exception and logbook is tested by default and there is no specific logic from your code.如果您的类中有 np 方法,那么最好将此类添加为异常,并且默认情况下会测试日志,并且您的代码中没有特定的逻辑。

Unfortunately there is no way to exclude toString() from generated builders.不幸的是,无法从生成的构建器中排除toString()

But you can configure your lombok to generate code which will be ignored by JaCoCo.但是您可以配置您的 lombok 以生成将被 JaCoCo 忽略的代码。 It is useful to exclude from test code coverage not only toString() methods but also all getters, setters, builders, and other staff generated by lombok.从测试代码覆盖率中排除toString()方法以及所有由 lombok 生成的 getter、setter、builder 和其他人员很有用。

  1. Add lombok.config file into your root project dirlombok.config文件添加到您的根项目目录中
  2. Add the following lines into it:将以下行添加到其中:
        config.stopBubbling = true
        lombok.addLombokGeneratedAnnotation = true
  1. Rebuild your project重建你的项目

More details about configuring lombok you can find here: https://projectlombok.org/features/configuration您可以在此处找到有关配置 lombok 的更多详细信息: https : //projectlombok.org/features/configuration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM