Disable sending null fields via Rest Assured in runtime

Viewed 3597

Lets assume that I got following model:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class MyModel {
    private Object field; //required field
    private Object anotherField; //optional field
}

Now I would like to verify if my REST endpoint is working properly (is expecting only required field), so I would like to perform following 2 requests and check if they result in 200:

{
    "field": "someValue",
    "anotherField": null
}

and:

{
    "field": "someValue"
}

With the first one, the code is simple:

MyModel payload = MyModel.builder().field("someValue").build();
when().contentType(ContentType.JSON).body(payload).put("http://my-service.com/myendpoint/").then().statusCode(200);

But the second one is starting to be a little bit crippy. If I won't provide value for anotherField, by default it will be null, and thats what will be sent via REST (first TCs). But at this moment I don't want to send this field. In other words:

I want RestAssured to don't send nulls. Is there any way to achieve that?

3 Answers
Related