Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java lombok unit tests for getters setters

I am using lombok in my Java project annotating my object's private member variables with @Getter and @Setter annotations. We know that those getters and setters would work as expected. So we don't really need those to be unit tested. However, that brings down the code coverage of the code in unit testing.

Is there a way to tell the unit testing engine that the getters and setters generated by lombok need not be tested?

like image 743
Nik Avatar asked Jan 18 '17 18:01

Nik


People also ask

Should you write unit tests for getters and setters?

If you tests use the getters/setters to achieve their goal of testing the "real" functionality, then that's good enough. If, on the other hand, your getters and setters do more than just get and set (i.e. they're properly complex methods), then yes, they should be tested.

How do you bypass the getter setter in Lombok?

You can always manually disable getter/setter generation for any field by using the special AccessLevel. NONE access level. This lets you override the behaviour of a @Getter , @Setter or @Data annotation on a class.

Should we use @data Lombok?

Also, I really discourage using any Lombok annotation that modifies the code. If you take @Data, @Getter, @Setter, @AllArgsConstructor adds new codes into the existing source code without modifying the code we wrote.


1 Answers

From Lombok 1.16.14, all generated methods will be annotated with @lombok.Generated.

You can exclude them from the Cobertura test report:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>cobertura-maven-plugin</artifactId>
  <version>2.7</version>
  <configuration>
    <instrumentation>
      <ignoreMethodAnnotation>lombok.Generated</ignoreMethodAnnotation>
    </instrumentation>
  </configuration>
</plugin>

Disclosure: I am a Lombok developer.

NB. At this time, Lombok 1.16.14 has not been released. You can download the edge release that contains this fix though.

like image 141
Roel Spilker Avatar answered Nov 14 '22 23:11

Roel Spilker