【问题标题】:using junit5 assertAll to assert cucumber data table list of maps使用junit5 assertAll 断言黄瓜数据表列表地图
【发布时间】:2021-02-24 08:02:20
【问题描述】:

我在黄瓜步骤定义中有这个

 secondIT.jsonPathEvaluator = secondIT.response.jsonPath();

然后在另一个步骤 def 中,我断言,我有

  public void employee_response_equals(DataTable responseFields){
              List<Map<String,String>> fields = responseFields.asMaps(String.class,String.class);
              Iterator<Map<String, String>> it = fields.iterator();
              while (it.hasNext()) {
                     Map<String, String> map = it.next(); 
                    for (Map.Entry<String, String> entry : map.entrySet()) {
                      System.out.println(entry.getKey() + " = " + entry.getValue());
                      assertEquals(secondIT.jsonPathEvaluator.get(entry.getKey()), entry.getValue());
                     assertAll(
                        // how to use this instead
                );
                    
                 }
             }
            
        }

如何使用 junit5 assertAll 断言每个键获取的值(来自 jsonPathEvaluator)和来自映射的值

我尝试使用assertEquals,但我不确定这是否正确,因为它只打印一些信息,即key = data.id,当我评论该行时,它会打印所有内容

key = data.id
value = 2
key = data.employee_name
value = Garrett Winters

另外,我遇​​到了this,但我不确定如何为我的场景制作可执行文件

样本数据表:

  And response includes the following employee info
       |key                         |value| 
       | data.id                    | 3 |
       | data.employee_name         | Garrett Winters   |

和示例响应:

{
    "status": "success",
    "data": {
        "id": 2,
        "employee_name": "Garrett Winters",
        "employee_salary": 170750,
        "employee_age": 63,
        "profile_image": ""
    },
    "message": "Successfully! Record has been fetched."
}

【问题讨论】:

    标签: java junit junit5 cucumber-jvm


    【解决方案1】:

    assertAll 是错误的工作工具。仅当您确切知道要断言多少事物并且可以对它们进行硬编码时,它才有效。你可能想看看使用断言库,例如AssertJ

    在使用 AssertJ 之前,必须将问题简化一点。

    您可以在步骤中从数据表中删除标题:

        And response includes the following employee info
          | data.id            | 3               |
          | data.employee_name | Garrett Winters |
    

    然后可以tell Cucumber you want this data as map by changing the DataTable to a map。 Cucumber 会告诉你它是否无法满足你的需求。

    @Then("response includes the following employee info")
    public void employee_response_equals(Map<String, String> expectedFields){
    

    一旦您将信息作为地图,您就可以使用预期的键从 json 响应中收集所有键。

    Map<String, Object> actualFields = expectedFields.keySet()
        .stream()
        .collect(Collectors.toMap(expectedKey -> expectedKey, expectedKey -> jsonPathEvaluator.get(expectedKey)));
    
    

    然后你可以使用 AssertJ 的assertThat 来比较两张地图。

    assertThat(actualFields).containsAllEntriesOf(expectedFields);
    

    当字段不匹配时,您会收到一条很好的消息:

    java.lang.AssertionError: 
    Expecting map:
     <{"data.employee_name"="Nora Jones", "data.id"="3"}>
    to contain:
     <[data.id=3, data.employee_name=Garrett Winters]>
    but could not find the following map entries:
     <[data.employee_name=Garrett Winters]>
    

    【讨论】:

    • 抱歉回复晚了。当我尝试上述方法时,我得到 Reference to 'get' is ambiguous, both 'get()' and 'get(String)' match 。另外,employeeInfo 是在空地图上方使用的吗?
    • 你必须用 lambda 替换方法引用。
    • employeeInfo 应该是expectedFields
    • Collectors.toMap 有两个参数。首先是流中的项目(期望的键)如何变成新地图的键。我们使用expectedKey -&gt; expectedKey,所以键不会改变。
    • 第二个参数是另一个 lambda,它将预期的键转换为映射中的值。 expectedKey -&gt; jsonPathEvaluator.get(expectedKey) 所以这里我们说值是jsonPathEvaluator 为预期键提供的值。
    猜你喜欢
    • 2017-04-09
    • 2021-11-24
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多