I am calling a third party REST endpoint.
Request sample
{
"body": {
"accountNumber": "12345"
},
"header": {
"username": "someusername",
"password": "somepassword"
}
}
I have created 3 bean classes
MyRequest.java
@Builder
@JsonDeserialize(builder = MyRequest.MyRequestBuilder.class)
public class MyRequest {
@JsonProperty("header")
private MyHeader header;
@JsonProperty("body")
private MyBody body;
}
MyBody.java
@Getter
@Builder
public class MyBody {
private String accountNumber;
}
MyHeader.java
@Getter
@Builder
public class MyHeader {
private String username;
private String password;
}
I'm creating request object using
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. For all the fields I get the error message "Not covered by tests".
Normally I add @Getter and @Setter for Response objects. For request, I just add @Builder annotation. In this case, if I remove @Getter from MyBody and MyHeader, the third party REST endpoint is getting null values. It looks like @Getter is invoked when setting the objects to MyRequest.java. 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? Any help is appreciated.
Create a lombok.config and add the following attribute to it.
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
You need to instruct Jackson somehow which data should be included during serialization. The default mechanism is to use getters for that purpose.
Replace @Getter with @JsonProperty to get 100% code coverage.
@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
Thanks @Alexander Ivanchenko
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.