You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Cucumber步骤中使用MultiValues Map及编写对应正则表达式

解决方案

步骤匹配正则

你原有用于匹配步骤文本的正则无需修改,直接保留如下规则即可匹配两种消息类型的步骤:

^the following fields are newly populated on (Vet Out|Vet Enriched) message with expected value$

多值Map的实现和步骤文本的正则匹配无关,所有多值转换逻辑都在后续的DataTable处理环节完成。

多值Map转换实现

步骤正则匹配通过后,Cucumber会自动将步骤后的表格作为DataTable参数传入方法,你只需要添加转换逻辑将表格第二列的逗号分隔值拆分存入List,即可得到Map<String, List<String>>类型的多值Map,Java版示例如下:

import io.cucumber.java.en.Given;
import io.cucumber.datatable.DataTable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Given("^the following fields are newly populated on (Vet Out|Vet Enriched) message with expected value$")
public void checkMessageFields(String messageType, DataTable dataTable) {
    Map<String, List<String>> multiValueMap = new HashMap<>();
    // 无表头表格用raw()遍历每一行
    dataTable.raw().forEach(row -> {
        String fieldKey = row.get(0).trim();
        // 按逗号加任意空格拆分值,自动去除前后空格
        List<String> fieldValues = Arrays.stream(row.get(1).split(",\\s*"))
                .map(String::trim)
                .toList();
        multiValueMap.put(fieldKey, fieldValues);
    });
    // 后续业务逻辑处理
}

这样处理后,你给出的示例表格| domain | VET, abc, xyz |会被转换为key=domain,value=[VET, abc, xyz]的键值对存入Map。


内容的提问来源于stack exchange,提问作者Gurpreet Flora

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 22:09:00