Warm tip: This article is reproduced from serverfault.com, please click

Cant find field when converting Cucumber DataTable to POJO using Jackson Fasterxml

发布于 2020-11-30 18:44:00

I'm trying to convert the following Datatable to POJO class using Jackson Faster XML but when my test runs I'm getting the following error. I'm not sure why the table isn't mapping. The customerId is present in the POJO class.

Error:

cucumber.runtime.CucumberException: No such field learning.pojo.customerId

Step Feature:

  And the user imported the following product file
      | customerId   | ....
      |customer1     | ....

Step Java:

@When("^the user imported the following product file$")
public void uploadFile(DataTable table) throws IOException {
     Example productImportFileModel = table.asList(Example.class).get(0);

Root POJO:

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
                           "products"
                   })
public class Example{
    
    @JsonProperty("products")
    private List<Products> products = null;

    @JsonProperty("products")
    public List<Products> getProducts() {
        return products;
    }

    @JsonProperty("products")
    public void setProducts(List<Products> products) {
        this.products = products;
    }
}

Products POJO:

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
                           "customerId",
                           .....
                           .....
                           .....
                           .....
                           .....

                   })
public class Products {
    
    @JsonProperty("customerId")
    private String customerId;

    @JsonProperty("customerId")
    public String getCustomerId() {
        return customerId;
    }
    
    @JsonProperty("customerId")
    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }
                           .....
                           .....
                           .....
                           .....
                           .....
}
Questioner
stuckAsFuk
Viewed
0
M.P. Korstanje 2020-12-01 07:31:03

The json representation of Example is:

{
  "products": [ ..... ]
}

The json representation of Products is:

{
  "customerId": "customer1"
   ....
}

The json representation of the table as a list is:

[ 
  {
    "customerId": "customer1"
     ....
  }
]

So consider using:

Product productImportFileModel = table.asList(Product.class).get(0);

Or better yet:

@When("^the user imported the following product file$")
public void uploadFile(List<Product> products) throws IOException {
     Product productImportFileModel = products.get(0);

You can debug this by asking Jackson for the json representation:

JsonNode json = table.asList(JsonNode.class).get(0);
System.out.println(json);