Home > Back-end >  When using Rest Assured, if I send a post request to submit data to a server, how can I intentionall
When using Rest Assured, if I send a post request to submit data to a server, how can I intentionall

Time:06-30

Given this simple class representing a Person object:

@Builder
public class Person {
         int age;
         String name;
    }

Using builder to create an instance of Person and posting it to a server using Rest Assured:

public class main {
     public static void main(String [] args){
          Person p1 = Person.builder().name("John").build();
          Person p2 = Person.builder().age(29).build();
          //CODE TO SEND POST REQUEST USING REST ASSURED
     }
}

As I understand it, the corresponding json string being received by the server would look something like this:

p1: { "name": "John", "age": 0 }
p2: { "name": null, "age": 29 }

My question is: How can I intentionally leave out certain attributes of a class? Lets say that the endpoint which I am posting this data to requires that the schema contains an age or name attribute, but for testing purposes, I want to leave out the age attribute so that what is received looks something like this:

p1: { "name": "John" }
p2: { "age": 29 }

I am thinking I could create a separate, similar class representing a person, but removing the attribute in question. This just seems wrong and inefficient. Is there a better way? Or is there some sort of stuff going on under the hood that by intentionally uninitializing an attribute, its left out in the post request? Thanks for your time.

CodePudding user response:

It depends on how you are sending the Person class, but you could opt to filter out the null values by using @JsonInclude(Include.NON_NULL), or have a different mapper configured depending on your use case.

This answer describes a way how to do this with either Jackson or Gson.

  • Related