简体   繁体   中英

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 "

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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