0

Pls, I need help, I'm getting a null pointer exception, When I send a request, I'm catching values in Cucumber through the List<Map<String, String>>.

I'm testing API with Rest Assured and Cucumber, and I have the following POGO classes:

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

 @Data

 public class ProductRequest {

    private String address;

    private double latitude;

    private boolean active;

    private List<ProductTypes> productTypes;

    private List<ProductVideos> productVideos;

    private String shortDescription;

    private String fullDescription;

    private boolean mostPopular;

    private String calendarColor;

    private String name;

    private List<ProductWorkingDates> productWorkingDates;

    private double longitude;

}

 import lombok.Data;

@Data

public class ProductTypes {

    private String name;
    private String id;
}

 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 Integer usedQuantity;
    private boolean active;
    private String deletedAt;
    private String dayOfWeek;
}

And I have my Cucumber Scenarios

Feature: Creating a new product
 
  Scenario: provider creates a new product
    Given product is set with the following fields
      | name             | active | shortDescription      | fullDescription               | mostPopular | longitude | latitude     | calendarColor | address |
      | AutoTestPositive | true   | Test For Automation 1 | Automation for API in Postman | true        | 456.456   | 3245678.5467 | blue          | Bishkek |

  Scenario: provider enters a data in productTypes
    Given product types is set with following fields
      | name    | id       |
      | TestAPI | 5bpJ0olV |

  Scenario: provider enters a data in ProductWorkingDates
    Given product Working Dates is set with following fields
      | fromDate        | toDate          | name                | strictHours | deletedAt | maxUsedTicketsQuantity | errorCode |
      | Thu May 27 2021 | Sat Dec 31 2022 | 1234567sdfgh#$#%^%& | false       |           | 0                      | 0         |

  Scenario: provider enters a data in time Slots
    Given product time Slots is set with the following fields
      | active | endTime  | dayOfWeek | duration | quantity | startTime | usedQuantity |
      | false  | 20:00:00 | Monday    | 01:00    | 67       | 09:00:00  | 0            |

 
  Scenario: Provider creates a new product
    Given provider creates a new product

And I have My StepDefenition class

public class CreateNewProductSteps {

    private Response response;
    public ProductRequest productRequest ;

    @Before
    public void setUp() {
        RestAssured.baseURI = PropertiesReader.getPropertiesValue("Book_It_BaseURL");

    }

    @Given("^provider creates a new product$")
    public void providerCreatesANewProduct() {

        String newProd = ObjectConverter.convertObjectToJson(productRequest);
        RestHttpRequest.addHeaders();

        response = RestHttpRequest
                .requestSpecification
                .body(newProd)
                .when()
                .request(String.valueOf(RestHttpRequest.HttpMethods.POST), "/api/product");
    }

    @Given("^product is set with the following fields$")
    public void productIsSetWithTheFollowingFields(List<Map<String, String>> product) {

        productRequest.setName(product.get(0).get("name"));
        productRequest.setActive(Boolean.parseBoolean(product.get(0).get("active")));
        productRequest.setShortDescription(product.get(0).get("shortDescription"));
        productRequest.setFullDescription(product.get(0).get("fullDescription"));
        productRequest.setMostPopular(Boolean.parseBoolean(product.get(0).get("mostPopular")));
        productRequest.setLongitude(Double.parseDouble(product.get(0).get("longitude")));
        productRequest.setLatitude(Double.parseDouble(product.get(0).get("latitude")));
        productRequest.setCalendarColor(product.get(0).get("calendarColor"));
        productRequest.setAddress(product.get(0).get("address"));

    }

    @Given("^product types is set with following fields$")
    public void productTypesIsSetWithFollowingFields(List<Map<String, String>> productTypes) {

        ProductTypes productTypes1 = new ProductTypes();
        productTypes1.setId(productTypes.get(0).get("id"));
        productTypes1.setName(productTypes.get(0).get("name"));

        List<ProductTypes> productTypesList = new ArrayList<>();
        productTypesList.add(productTypes1);
        productRequest.setProductTypes(productTypesList);
    }

    @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")));
        productWorkingDates1.setDeletedAt(productWorkingDates.get(0).get("deletedAt"));
        productWorkingDates1.setMaxUsedTicketsQuantity(Integer.parseInt(productWorkingDates.get(0).get("maxUsedTicketsQuantity")));
        productWorkingDates1.setErrorCode(Integer.parseInt(productWorkingDates.get(0).get("errorCode")));

        List<ProductWorkingDates> listProductWorkingDates = new ArrayList<>();
        listProductWorkingDates.add(productWorkingDates1);
        productRequest.setProductWorkingDates(listProductWorkingDates);
    }

    @Given("^product time Slots is set with the following fields$")
    public void productTimeSlotsIsSetWithTheFollowingFields(List<Map<String, String>> timeSlots) {

        ProductWorkingDates productWorkingDates1 = new ProductWorkingDates();

        TimeSlots timeSlots1 = new TimeSlots();
        timeSlots1.setActive(Boolean.parseBoolean(timeSlots.get(0).get("active")));
        timeSlots1.setEndTime(timeSlots.get(0).get("endTime"));
        timeSlots1.setDayOfWeek(timeSlots.get(0).get("dayOfWeek"));
        timeSlots1.setDuration(timeSlots.get(0).get("duration"));
        timeSlots1.setQuantity(timeSlots.get(0).get("quantity"));
        timeSlots1.setStartTime(timeSlots.get(0).get("startTime"));
        timeSlots1.setUsedQuantity(Integer.parseInt(timeSlots.get(0).get("usedQuantity")));

        List<TimeSlots> timeSlotsList = new ArrayList<>();
        timeSlotsList.add(timeSlots1);
        productWorkingDates1.setTimeSlots(timeSlotsList);

        List<ProductWorkingDates> listProductWorkingDates = new ArrayList<>();
        listProductWorkingDates.add(productWorkingDates1);

        productRequest.setProductWorkingDates(listProductWorkingDates);

    }

This is the exception:

java.lang.NullPointerException
    at www.logisense.com.cucumber.steps.newProduct.CreateNewProductSteps.productIsSetWithTheFollowingFields(CreateNewProductSteps.java:54)
    at ✽.Given product is set with the following fields(src/test/resources/features/wizard/wizardNewProduct.feature:7)


  Scenario: provider creates a new product         # src/test/resources/features/wizard/wizardNewProduct.feature:6
    Given product is set with the following fields # CreateNewProductSteps.productIsSetWithTheFollowingFields(String,String>>)
      java.lang.NullPointerException
        at www.logisense.com.cucumber.steps.newProduct.CreateNewProductSteps.productIsSetWithTheFollowingFields(CreateNewProductSteps.java:54)
        at ✽.Given product is set with the following fields(src/test/resources/features/wizard/wizardNewProduct.feature:7)
      

java.lang.NullPointerException
    at www.logisense.com.cucumber.steps.newProduct.CreateNewProductSteps.productTypesIsSetWithFollowingFields(CreateNewProductSteps.java:80)
    at ✽.Given product types is set with following fields(src/test/resources/features/wizard/wizardNewProduct.feature:12)

1 Answer 1

1

You're not initializing your productRequest. So lines like:

productRequest.setName(...);

or

productRequest.setProductTypes(...);

obviously fail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.