不包括 Lombok @Builder 自带的 toString() 方法

huangapple go评论73阅读模式
英文:

Exclude toString() method that comes with Lombok @Builder

问题

根据这个Lombok文档,通过使用***@Builder***注解来生成7个内容,其中之一是“一个合理的toString()实现”。

在我正在处理的项目中,这个默认的***toString()**方法实际上引起了一些麻烦,jacoco报告认为该方法未经测试,事实上确实如此,因为我并不打算有一个toString()*方法。

有没有办法避免生成默认的*toString()*方法,如果有,该如何实现?

非常感谢任何想法或洞见。

英文:

According to this Lombok documentation, there are 7 things generated by annotating a class with @Builder, one of them being "A sensible toString() implementation"

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.

Is there a way to avoid the generation of the default toString() method, and if so, how?

Any thoughts or insight would be much appreciated.

答案1

得分: 1

很遗憾,无法从生成的构建器中排除 toString() 方法。

但是,您可以配置您的 Lombok 以生成 JaCoCo 忽略的代码。这对于排除测试代码覆盖率不仅包括 toString() 方法,还包括所有的 getters、setters、builders,以及其他由 Lombok 生成的内容非常有用。

  1. 在您的根项目目录中添加 lombok.config 文件。
  2. 将以下内容添加到文件中:
        config.stopBubbling = true
        lombok.addLombokGeneratedAnnotation = true
  1. 重新构建您的项目。

关于配置 Lombok 的更多详细信息,请参阅:https://projectlombok.org/features/configuration

英文:

Unfortunately there is no way to exclude toString() from generated builders.

But you can configure your lombok to generate code which will be ignored by 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.

  1. Add lombok.config file into your root project dir
  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

答案2

得分: 0

我认为你不能移除toString(),但你总是可以测试并验证是否存在空指针异常。

如果你的类中有np方法,最好将这个类添加为异常,日志记录默认已经经过测试,而且没有来自你的代码的特定逻辑。

英文:

I don't think you can remove toString(), but you can always just test it and validate that there is no nullPointerException throne.

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.

huangapple
  • 本文由 发表于 2020年9月3日 02:14:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63711372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定