简体   繁体   中英

Testing Lombok annotations with Junit

I have a simple Java program which uses the Lombok Annotation - @Builder . I am testing my code using junit and everytime I run my unit tests, my coverage is always below 50% inspite of the fact that I am testing my entire code.

I looked into the junit generated code coverage and I saw that it was the Lombok annotation which was making the coverage drop.

I see something like:

toString() - 0%
build() - 0%
MyMethod.MyMethodBuilder() - 0%

How do I test these methods for the @Builder annotation? Or the only way to improve coverage is to exclude those from the test coverage?

Starting JaCoCo v0.8.0, you should create lombok.config file to the root of your project and add following content to it:

lombok.addLombokGeneratedAnnotation = true

This will add @Generated annotation to Lombok generated files and JaCoCo will know that code coverage of such a code should be ignored.

Credits to original author here

Decompile class which contains lombok annotation to see how lombok has generated source code and then write junits considering that code. eg In case of @ToString, make explicit call to toString method on class object ie obj.toString();

Create lombok.config file and add texts below:

config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true

config.stopBubbling = true is telling Lombok that this is the root directory and that it shouldn't search parent directories for more configuration files (you can have more than one lombok config files in different directories/packages).

lombok.addLombokGeneratedAnnotation = true is telling Lombok to add the @lombok.Generated annotation to all generated methods.

最好使用 pl.pojo 库来测试 pojo 模型类。

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