从黄瓜java中的Scenario outline的示例表中解析整数列表

问题描述 投票:1回答:1

我必须使用带有java的黄瓜编写BDD测试,我想从我的示例表的每一行解析整数列表,并使用最新版本的黄瓜(4.2.6)将此列表传递给我的步骤方法。所以我在我的功能文件中得到了以下步骤定义:

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: (\\d+.)")
    public void storeIntegerList(List<Integer> integers) {
     System.out.println(integers.size());
    }

不幸的是,我找不到一种方法来处理将这些值解析成列表。它要么找不到步骤方法(我尝试过很多不同的正则表达式),要么抛出异常,告诉我我的数字无法转换为列表。

作为一种解决方法,我将列表解析为字符串然后拆分它。但是,我无法想象在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());        
    }

java cucumber cucumber-jvm cucumber-java cucumber-junit
1个回答
4
投票

我做了@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());
    }
}

之后我可以在我的步骤类中执行以下操作:

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

功能文件可以像这样使用:

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

    Examples:
      | integer_list     |
      | 2, 3, 5, 6       | 
      | 3, -12, 45, -5, 6| 
© www.soinside.com 2019 - 2024. All rights reserved.