TwinAxe96
TwinAxe96

Reputation: 139

Cucumber passing Entity with a Map field. How to map the variables send from feature file

i am trying to pass an Entity that has only 1 field, which is a Map>. I am having a trouble figuring out how to make cucumber pass the variables from the feature file in the Entity.

Here is the Entity object:

public class Page {
    // change the List to represent a List of Questions when the class is implemented.
    private Map<Integer, List<String>> questions;

    public Map<Integer, List<String>> getQuestions() {
        return questions;
    }

    public void setQuestions(Map<Integer, List<String>> questions) {
        this.questions = questions;
    }
}

And the feature file:

Scenario: Create new Questionnaire
    Given the user has clicked the "New questionnaire" button
    When the user creates new questionnaire:
      | name |
      | [6A] |
    And the user completes the questionnaire design form:
      | title                 |
      | fitness questionnaire |
    And completes the questionnaire structure:
      | questionsPage | questions                                                                         |
      | 1             | {Q1 - NPS Question, Q2 - Fitness Driver Question, 3 - Feedback Request Question } |
      | 2             | {Q3 - Feedback Request Question}                                                  |
    Then questionnaire is created successfully

The step definition:

@And("^completes the questionnaire structure:$")
    public void completesTheQuestionnaireStructure(List<Page> pages) throws Throwable {
        questionnaireCreator.createQuestionnaireStructure(pages.get(0));
    }

I want to set the key of the map as the int that i am passing from the scenario and the value behind the key the specific list of questions, currently it is throwing an Exception: "cucumber.runtime.CucumberException: Duplicate field questions"

Anyone have faced such an issue how did you proceed to fix it?

Upvotes: 1

Views: 4189

Answers (1)

Grasshopper
Grasshopper

Reputation: 9058

Not sure about mixing serenity and cucumber. You can try the below solution if it works.

Also I am assuming from github and maven that this version still supports only cucumber 2 so XStream will work.

Need to remove the header from the datatable and modify the question list. And completes the questionnaire structure:

  | 1 | Q1 - NPS Question,Q2 - Fitness Driver Question,Q3 - Feedback Request Question |
  | 2 | Q3 - Feedback Request Question                                                |

Use this in the stepdefinition code. Cucumber by itself will not be able to convert into a List as a key in a map. You can use the new map to set into the Page object.

@And("^completes the questionnaire structure:$")
    public void completesTheQuestionnaireStructure(Map<Integer, String> pages) throws Throwable {

        Map<Integer, List<String>> pageNew = new HashMap<>();
        pages.forEach((k, v) -> pageNew.put(k, Arrays.asList(v.split(","))));

        System.out.println(pageNew);
    }

Upvotes: 1

Related Questions