从黄瓜Java中的场景大纲示例表中解析整数列表

Kri*_*zsa 1 java cucumber cucumber-jvm cucumber-junit cucumber-java

我必须使用Java黄瓜来编写BDD测试,我想从示例表的每一行中解析一个整数列表,然后使用最新版本的Cucumber(4.2.6)将此列表传递给我的step方法。因此,我在功能文件中获得了以下步骤定义:

Scenario Outline: Some scenarion
    Given a list of integer: <integer_list> 

    Examples:
      | integer_list   |
      | 2, 3, 5, 6     | 
      | 3, 12, 45, 5, 6| 
Run Code Online (Sandbox Code Playgroud)

我的代码中需要这样的内容:

   @Given("a list of integer: (\\d+.)")
    public void storeIntegerList(List<Integer> integers) {
     System.out.println(integers.size());
    }
Run Code Online (Sandbox Code Playgroud)

不幸的是,我找不到一种将这些值解析为列表的方法。要么找不到step方法(我尝试过很多不同的正则表达式),要么抛出异常,通知我我的数字无法转换为列表。

解决方法是将列表解析为字符串,然后将其拆分。但是,我无法想象在2019年没有更好的方法可以做到这一点。

解决方法:

Scenario Outline: Some scenarion
    Given a list of integer: "<integer_list>" 

    Examples:
      | integer_list   |
      | 2, 3, 5, 6     | 
      | 3, 12, 45, 5, 6| 


@Given("a list of integer: {string}")
    public void storeIntegerList(String integers) {
        List<String> integersAsString = Arrays.asList(integers.split(","));
        List<Integer> integerList = integersAsString.stream().map(s -> Integer.valueOf(s.trim())).collect(Collectors.toList());
        System.out.println(integerList.size());        
    }

Run Code Online (Sandbox Code Playgroud)

Kri*_*zsa 6

我按照@Grasshopper的建议进行操作,并实现了我自己的TypeRegistryConfigurer,并将其放在我的流氓类旁边(位于Glue路径上):

import cucumber.api.TypeRegistry;
import cucumber.api.TypeRegistryConfigurer;
import io.cucumber.cucumberexpressions.ParameterType;

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

import static java.util.Locale.ENGLISH;

public class TypeRegistryConfiguration implements TypeRegistryConfigurer {
    @Override
    public Locale locale() {
        return ENGLISH;
    }

    @Override
    public void configureTypeRegistry(TypeRegistry typeRegistry) {
        typeRegistry.defineParameterType(new ParameterType<>(
                "integerList",  //this name can be used in the step method
                "(-?[0-9]+(,\\s*-?[0-9]+)*)", //regexp to match to a comma separated integer list which can contain negative numbers and whitespaces as well
                List.class,  //the expected parameter type
                this::transform  // equivalent to (String s) -> this.transformer(s), this is the transformer method which will be used to create the desired step parameter 
        ));
    }

//transforms the string form to an integer list
    private List<Integer> transform(String integers) {
        List<String> integersAsString = Arrays.asList(integers.split(","));
        return integersAsString.stream().map(s -> Integer.valueOf(s.trim())).collect(Collectors.toList());
    }
}

Run Code Online (Sandbox Code Playgroud)

之后,我就可以在Step类中执行以下操作:

   @Given("a list of integer: {integerList}")
    public void storeIntegerList(List<Integer> integers) {
     System.out.println(integers.size());
    }
Run Code Online (Sandbox Code Playgroud)

功能文件可以这样使用:

Scenario Outline: Some scenarion
    Given a list of integer: <integer_list> 

    Examples:
      | integer_list     |
      | 2, 3, 5, 6       | 
      | 3, -12, 45, -5, 6| 
Run Code Online (Sandbox Code Playgroud)