Skip to content
Advertisement

Exception in thread “main” java.lang.ClassCastException: www.logisense.com.pogos.wizardNewProduct.ProductWorkingDates cannot be cast to java.util.List

I’m trying to convert Object to JSON, I have the following POGO classes:

import lombok.Data;
import java.util.List;

@Data
public class CreateNewProductRequest {

    private List<ProductIncludedTags> productIncludedTags;

    private List<ProductWorkingDates> productWorkingDates;

    private double longitude;

    private List<ProductTags> productTags;

}

import Lombok.Data;
import java.util.List;

@Data
public class ProductWorkingDates {

    private String fromDate;
    private String toDate;
    private String name;
    private Boolean strictHours;
    private List<TimeSlots> timeSlots;
    private String deletedAt;
    private Integer maxUsedTicketsQuantity;
    private  Integer errorCode;
}


import lombok.Data;

@Data
public class TimeSlots {

    private String startTime;
    private String endTime;
    private String duration;
    private String quantity;
    private String usedQuantity;
    private boolean active;
    private String deletedAt;
}

I’m passing the data through cucumber, and I have the following Scenario

Scenario: Provider enters a data in ProductWorkingDates

Given product Working Dates is set with following fields
  | fromDate        | toDate          | name                | strictHours |
  | Thu May 27 2021 | Sat Dec 31 2022 | 1234567sdfgh#$#%^%& | false       |

and I have my step Defenition class, where I’m trying to catch the data through the List<Map<String, String>> productWorkingDates;

    @Given("^product Working Dates is set with following fields$")
    public void productWorkingDatesIsSetWithFollowingFields(List<Map<String,String>> productWorkingDates) {
        ProductWorkingDates productWorkingDates1 = new ProductWorkingDates();
        productWorkingDates1.setFromDate(productWorkingDates.get(0).get("fromDate"));
        productWorkingDates1.setToDate(productWorkingDates.get(0).get("toDate"));
        productWorkingDates1.setName(productWorkingDates.get(0).get("name"));
        productWorkingDates1.setStrictHours(Boolean.parseBoolean(productWorkingDates.get(0).get("strictHours")));
       


//====> this is where I'm having an issue, pls help 

productRequest.setProductWorkingDates((List<ProductWorkingDates>) productWorkingDates1);
  

  }

Advertisement

Answer

From what I can see, productWorkingDates1 is not a list, but rather an element of a list, of type ProductWorkingDates : ProductWorkingDates productWorkingDates1 = new ProductWorkingDates();

When trying to cast from ProductWorkingDates to List, you are facing the exception you have described : (List<ProductWorkingDates>) productWorkingDates1

What you would need is something looking like this :

List<ProductWorkingDates> listProductWorkingDates = new ArrayList<>();
listProductWorkingDates.add(productWorkingDates1);
productRequest.setProductWorkingDates(listProductWorkingDates);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement