使用 junit5 assertAll 断言黄瓜数据 table 地图列表
using junit5 assertAll to assert cucumber data table list of maps
我在黄瓜步骤定义中有这个
secondIT.jsonPathEvaluator = secondIT.response.jsonPath();
然后在不同的步骤定义中,我断言,我有
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
)和来自 map
的值
我尝试使用 assertEquals
但我不确定这是否正确,因为它只打印一些信息,即这个 key = data.id
当我评论那一行时它打印所有内容
key = data.id
value = 2
key = data.employee_name
value = Garrett Winters
此外,我遇到了 ,但我不确定如何为我的场景制作 executables
样本数据table:
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."
}
assertAll
是错误的工具。仅当您确切知道要断言的数量并且可以对这些进行硬编码时,它才有效。您可能想要查看使用断言库,例如 AssertJ.
在使用 AssertJ 之前,问题必须稍微简化。
您可以在您的步骤中从数据 table 中删除 header:
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]>
我在黄瓜步骤定义中有这个
secondIT.jsonPathEvaluator = secondIT.response.jsonPath();
然后在不同的步骤定义中,我断言,我有
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
)和来自 map
我尝试使用 assertEquals
但我不确定这是否正确,因为它只打印一些信息,即这个 key = data.id
当我评论那一行时它打印所有内容
key = data.id
value = 2
key = data.employee_name
value = Garrett Winters
此外,我遇到了
样本数据table:
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."
}
assertAll
是错误的工具。仅当您确切知道要断言的数量并且可以对这些进行硬编码时,它才有效。您可能想要查看使用断言库,例如 AssertJ.
在使用 AssertJ 之前,问题必须稍微简化。
您可以在您的步骤中从数据 table 中删除 header:
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]>