How to pass an array of Integers from Cucumber features file to step definition while writing unit test cases
Asked Answered
H

1

9

I am learning Cucumber for unit testing and was trying to write unit tests for the Radix Sort code. But I am unable to figure out how to provide array of integers as an input to radix sort code from feature file.

I tried providing below input:

  Scenario: Sorting integer array using radix sort within the specified range
    Given The nonnull integer array 10,25,0,1
    When radix sort is performed over the range from 0 to 7
    Then validate if the array is sorted

For the above scenario cucumber expects below-mentioned code body:

@Given("The nonnull integer array {double}")
public void the_nonnull_integer_array(Double double1) {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

If I try giving input as

    Scenario: Sorting integer array using radix sort within the specified range
    Given The nonnull integer array [10,25,0,1]
    When radix sort is performed over the range from 0 to 7
    Then validate if the array is sorted

then cucumber expects below-mentioned code body:

@Given("The nonnull integer array [{double}]")
public void the_nonnull_integer_array(Double double1) {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

I also tried providing array within quotes

  Scenario: Sorting integer array using radix sort within the specified range
    Given The nonnull integer array "[10,25,0,1]"
    When radix sort is performed over the range from 0 to 7
    Then validate if the array is sorted

But then cucumber expects String as an input

@Given("The nonnull integer array {string}")
public void the_nonnull_integer_array(String string) {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

I tried various other ways without any success. Can anyone suggest any better approach on handling such testing scenario?

Higher answered 23/5, 2020 at 18:24 Comment(0)
A
13

There are lots of ways to do this!

Feature: Lists of integers

  Scenario: Passing lists of integers
    * With individual arguments 1, 2, 3
    * With a custom parameter type [1, 2, 3]
    * With a horizontal datatable
      | 1 | 2 | 3 |
    * With a vertical datatable
      | 1 |
      | 2 |
      | 3 |
    * With a horizontal list
      | 1 | 2 | 3 |
    * With a vertical list
      | 1 |
      | 2 |
      | 3 |
package com.example;

import io.cucumber.datatable.DataTable;
import io.cucumber.java.ParameterType;
import io.cucumber.java.Transpose;
import io.cucumber.java.en.Given;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class StepDefinitions {

    @Given("With individual arguments {int}, {int}, {int}")
    public void with_individual_arguments(Integer int1, Integer int2, Integer int3) {
        assertEquals(asList(int1, int2, int3), asList(1, 2, 3));
    }

    @ParameterType("\\[([0-9, ]*)\\]")
    public List<Integer> listOfIntegers(String integers) {
        return Arrays.stream(integers.split(", ?"))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
    }

    @Given("With a custom parameter type {listOfIntegers}")
    public void with_a_custom_parameter_type(List<Integer> list) {
        assertEquals(list, asList(1, 2, 3));

    }

    @Given("With a horizontal datatable")
    public void with_a_horizontal_datatable(@Transpose DataTable table) {
        assertEquals(table.column(0), asList("1", "2", "3"));
    }

    @Given("With a vertical datatable")
    public void with_a_vertical_datatable(DataTable table) {
        assertEquals(table.column(0), asList("1", "2", "3"));
    }

    @Given("With a horizontal list")
    public void with_a_horizontal_list(@Transpose List<Integer> list) {
        assertEquals(list, asList(1, 2, 3));
    }

    @Given("With a vertical list")
    public void with_a_vertical_list(List<Integer> list) {
        assertEquals(list, asList(1, 2, 3));
    }

}

The reason this doesn't work for you:

    Given The nonnull integer array 10,25,0,1

Cucumber is trying to be helpful. Because it sees 10,25 it thinks you'll probably want a double parameter in a step definition and suggests that. If you add a space after each number Cucumber will suggest the a step definition that uses an integer.

Worth reading:

Anglo answered 23/5, 2020 at 23:5 Comment(1)
try this link: github.com/cucumber/cucumber-expressions#readmeClientage

© 2022 - 2024 — McMap. All rights reserved.