我必须验证一条错误消息是否已填充10个字段,错误消息中将同时提及10个字段名称。
示例:
Error message:10 fields got over lapped and 10 field names are ac, bc, dc..
请帮助我编写逻辑以验证包含所有随机字符串的错误消息。
需要将所有字符串值传递到创建字符串中以比较错误消息。
Map<String,String> m = arg1.asMap(String.class,String.class);
System.out.println("\nFilling form with below data\n");
for( String fieldname : m.keySet())
{
//Need to pass this field name to the error message and the error message may have multiple fieldnames//
System.out.println("Key -> " + fieldnames + " Value -> " + m.get(fieldnames));
}
}
答案 0 :(得分:1)
您可以按以下方式获取键和值,here是如何使用Map
和EntrySet
的很好的答案:
Map<String,String> map = arg1.asMap(String.class,String.class);
for (Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
输出:
key1=value1
key2=value2
.
.
.
回答有关如何声明它们的问题:
List<String> fieldNames = new ArrayList<String>();
Map<String,String> map = arg1.asMap(String.class,String.class);
int i = 0;
for (Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
fieldName.add(entry.getKey());
i++;
}
Assert.assertEquals(errorMessage, "Error message:" + i + "fields got over lapped and" + i + "field names are" + fieldName.toString() );