Search code examples
jsonjacksonrest-assured

Passing an empty JSON array as a parameter to an @JsonProperty annotated method


I'm trying to pass the an empty array value in my payload for a Rest Assured test so that it contains:

{
  "areaCode": []
}

I have a POJO representing the data object:

@Data
public class AreaDetails {

//declare fields here
@JsonProperty("areaCode")
private JsonArray areaCode;

}

Then trying to set the value in my test:

@Test
public void updateAreaCodes()  {

    a = new AreaCodes();

    //set other fields
    a.setDealerOptions(<PARAM>);

I'm unclear what to pass as the parameter here, or indeed if I should declare the field as a JsonArray


Solution

  • POJO :

    @Data
    public static class Example {
    
        @JsonProperty("areaCode")
        private List<Object> areaCode = null;
    
    }
    

    Test :

        Example e = new Example();
        e.setAreaCode(new JSONArray());
    
        String abc = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(e);
        System.out.println(abc);