简体   繁体   English

如何使用@Getter @Builder Lombok 注释覆盖 Java bean class 上的 100% Sonarqube 代码覆盖率

[英]How to cover 100% Sonarqube code coverage on Java bean class with @Getter @Builder Lombok annotation

I am calling a third party REST endpoint.我正在呼叫第三方 REST 端点。

Request sample索取样品

{
     "body": {
        "accountNumber": "12345"
     },
     "header": {
        "username": "someusername",
        "password": "somepassword"
     }
}

I have created 3 bean classes我创建了 3 个 bean 类

MyRequest.java我的请求.java

@Builder
@JsonDeserialize(builder =  MyRequest.MyRequestBuilder.class)
public class MyRequest {
    @JsonProperty("header")
    private MyHeader header;
    @JsonProperty("body")
    private MyBody body;
}

MyBody.java我的身体.java

@Getter
@Builder
public class MyBody {
    private String accountNumber;
}

MyHeader.java MyHeader.java

@Getter
@Builder
public class MyHeader {
    private String username;
    private String password;
}

I'm creating request object using我正在使用创建请求 object

MyBody body = MyBody.builder().accountNumber("12345").build();
MyHeader header = MyHeader.builder().username("someusername").password("somepassword").build();

MyRequest request = MyRequest.builder().body(body).header(header).build();

Everything is working as expected.一切都按预期工作。 The code coverage for MyRequest.java is 100% but my MyBody.java and MyHeader.java is not. MyRequest.java 的代码覆盖率是 100%,但我的 MyBody.java 和 MyHeader.java 不是。 For all the fields I get the error message "Not covered by tests".对于所有字段,我收到错误消息“测试未涵盖”。

Normally I add @Getter and @Setter for Response objects.通常我为 Response 对象添加 @Getter 和 @Setter。 For request, I just add @Builder annotation.对于请求,我只是添加 @Builder 注释。 In this case, if I remove @Getter from MyBody and MyHeader, the third party REST endpoint is getting null values.在这种情况下,如果我从 MyBody 和 MyHeader 中删除@Getter,第三方 REST 端点将获得 null 值。 It looks like @Getter is invoked when setting the objects to MyRequest.java.看起来在将对象设置为 MyRequest.java 时调用了@Getter。 But for some reason it is not covered by my test cases.但出于某种原因,它没有包含在我的测试用例中。

How to make this work without @Getter or is there a way to cover all the fields (accountNumber, username and password) with @Getter annotation?如何在没有 @Getter 的情况下完成这项工作,或者有没有办法用 @Getter 注释覆盖所有字段(帐号、用户名和密码)? Any help is appreciated.任何帮助表示赞赏。

Create a lombok.config and add the following attribute to it.创建一个 lombok.config 并向其添加以下属性。

lombok.addLombokGeneratedAnnotation = true

For Maven projects, a file named lombok.config at project's basedir is the right spot, but other ways/locations may apply, see https://projectlombok.org/features/configuration对于 Maven 项目,项目 basedir 中名为 lombok.config 的文件是正确的位置,但其他方式/位置可能适用,请参阅https://projectlombok.org/features/configuration

You need to instruct Jackson somehow which data should be included during serialization.您需要以某种方式指示 Jackson 在序列化期间应包含哪些数据。 The default mechanism is to use getters for that purpose.默认机制是为此目的使用吸气剂。

Replace @Getter with @JsonProperty to get 100% code coverage.将 @Getter 替换为 @JsonProperty 以获得 100% 的代码覆盖率。

@Builder
public class MyBody {
    @JsonProperty
    private String accountNumber;
}

This is not my answer.这不是我的答案。 I got this answer from my other post Why 3rd party REST API's gets null values for request fields when removing @Getter Lombok annotation我从我的另一篇文章中得到了这个答案, 为什么第 3 方 REST API 在删除 @Getter Lombok 注释时获取请求字段的 null 值

Thanks @Alexander Ivanchenko谢谢@Alexander Ivanchenko

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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