簡體   English   中英

從黃瓜 java 中的場景大綱示例表中解析整數列表

[英]Parse integer list from Scenario outline's example table in cucumber java

我必須使用帶有 java 的黃瓜編寫 BDD 測試,我想從示例表的每一行解析一個整數列表,並使用最新版本的黃瓜 (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| 

我的代碼中需要這樣的東西:

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

不幸的是,我找不到將這些值解析為列表的方法。 它要么找不到 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());        
    }

我按照@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| 

對於 Cucumber 4+,有一種基於注釋的注冊新類型的方法。 對於整數列表,您將執行以下操作:

@ParameterType(name = "intList", value = "(-?\\d+(,\\s*-?\\d+)*)")
public List<Integer> defineIntegerList(String value) {
    // * -?\d+ : one or more digits, with optional negative sign at the start
    // For a list it will be one of the above digits with none or more groups of
    // "comma with digits", ignoring any whitespace before the digits
    return Arrays.stream(value.split(",")).map(s -> Integer.valueOf(s.trim()))
             .collect(Collectors.toList());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM