2

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.

Meng
  • 1,891
  • 1
  • 12
  • 10
  • 3
    If the JaCoCo report is the issue, one can use [`lombok.addLombokGeneratedAnnotation = true`](https://www.projectlombok.org/features/configuration) to annotate the generated code. It will then be ignored by JaCoCo. – Turing85 Sep 02 '20 at 18:16
  • Have a look at https://stackoverflow.com/questions/25592437/exclude-setters-and-getters-in-jacoco-code-coverage/61814482#61814482, since the generated toString() will show the same characteristics as getters and setter. – potame Sep 08 '20 at 13:32

2 Answers2

1

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

va1m
  • 31
  • 4
0

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.