使用Rest Assured检查JSON数组的值时出现问题。

huangapple 未分类评论81阅读模式
英文:

Problem with checking values of JSON array using Rest Assured

问题

我正在尝试使用Rest-Assured为我的应用程序添加一些测试,但我无法弄清楚如何断言一些嵌套值。错误消息如下:

期望:(包含“json”的集合)
实际:[[json,spring,gulp,path等...]]

以下是代码:

  1. when().
  2. get("/api/personsByID/{id}/{count}", 262, 2).
  3. then().
  4. statusCode(200).
  5. body("personDependencies.name", hasItems("json"));

以下是由rest控制器返回的JSON文件:

  1. [
  2. {
  3. "id": 346,
  4. "verified": true,
  5. "displayName": "eda656a2c3cb59ae840e40a28ba4ab50bfb9de0185abcb901c6af6dc59d6668f",
  6. "emails": [
  7. {
  8. "email": "16a23f2e5477df0bbcad718c3abc235b2cb8a1b6648d14f58d42a7be13df2b6e"
  9. }
  10. ],
  11. "personDependencies": [
  12. {
  13. "name": "json"
  14. },
  15. {
  16. "name": "spring"
  17. },
  18. {
  19. "name": "gulp"
  20. },
  21. {
  22. "name": "path"
  23. },
  24. ...
  25. // 其他项
  26. ...
  27. {
  28. "name": "javax.servlet"
  29. }
  30. ],
  31. "countries": [],
  32. "member_of": [],
  33. "projects": [],
  34. ...
  35. // 其他项
  36. ...
  37. "distance": 0.6028837702084446
  38. }
  39. ]

我不知道如何进行适当的断言,任何帮助都将是很好的。谢谢!

英文:

I'm trying to add some tests with Rest-Assured to my application, but I can't figure out how to assert some nested values. The error message is :
>Expected: (a collection containing "json")
Actual: [[json, spring, gulp, path etc...]]

Here is the code :

  1. when().
  2. get("/api/personsByID/{id}/{count}", 262, 2).
  3. then().
  4. statusCode(200).
  5. body("personDependencies.name", hasItems("json"));

And here is the JSON file that is returned by rest controller:

  1. [
  2. {
  3. "id": 346,
  4. "verified": true,
  5. "displayName": "eda656a2c3cb59ae840e40a28ba4ab50bfb9de0185abcb901c6af6dc59d6668f",
  6. "emails": [
  7. {
  8. "email": "16a23f2e5477df0bbcad718c3abc235b2cb8a1b6648d14f58d42a7be13df2b6e"
  9. }
  10. ],
  11. "personDependencies": [
  12. {
  13. "name": "json"
  14. },
  15. {
  16. "name": "spring"
  17. },
  18. {
  19. "name": "gulp"
  20. },
  21. {
  22. "name": "path"
  23. },
  24. {
  25. "name": "junit"
  26. },
  27. {
  28. "name": "activemq"
  29. },
  30. {
  31. "name": "hibernate"
  32. },
  33. {
  34. "name": "jstl"
  35. },
  36. {
  37. "name": "phantomjs"
  38. },
  39. {
  40. "name": "activiti"
  41. },
  42. {
  43. "name": "commons"
  44. },
  45. {
  46. "name": "h2"
  47. },
  48. {
  49. "name": "joda"
  50. },
  51. {
  52. "name": "log4j"
  53. },
  54. {
  55. "name": "exec"
  56. },
  57. {
  58. "name": "admin"
  59. },
  60. {
  61. "name": "coveralls"
  62. },
  63. {
  64. "name": "cxf"
  65. },
  66. {
  67. "name": "cglib"
  68. },
  69. {
  70. "name": "camel"
  71. },
  72. {
  73. "name": "sugaronrest"
  74. },
  75. {
  76. "name": "tslint"
  77. },
  78. {
  79. "name": "httpclient"
  80. },
  81. {
  82. "name": "guava"
  83. },
  84. {
  85. "name": "inventory"
  86. },
  87. {
  88. "name": "jackson"
  89. },
  90. {
  91. "name": "gson"
  92. },
  93. {
  94. "name": "event"
  95. },
  96. {
  97. "name": "OTRS"
  98. },
  99. {
  100. "name": "maven"
  101. },
  102. {
  103. "name": "karma"
  104. },
  105. {
  106. "name": "slf4j"
  107. },
  108. {
  109. "name": "postgresql"
  110. },
  111. {
  112. "name": "typescript"
  113. },
  114. {
  115. "name": "jasmine"
  116. },
  117. {
  118. "name": "spa"
  119. },
  120. {
  121. "name": "javax.servlet"
  122. }
  123. ],
  124. "countries": [],
  125. "member_of": [],
  126. "projects": [],
  127. "employee_type": [],
  128. "languages": [
  129. {
  130. "language": "reStructuredText",
  131. "sum": 575
  132. },
  133. {
  134. "language": "JSON",
  135. "sum": 21
  136. },
  137. {
  138. "language": "JavaScript",
  139. "sum": 4467
  140. },
  141. {
  142. "language": "Java",
  143. "sum": 7958
  144. },
  145. {
  146. "language": "Python",
  147. "sum": 2
  148. },
  149. {
  150. "language": "XML",
  151. "sum": 477
  152. },
  153. {
  154. "language": "Plain Text",
  155. "sum": 41
  156. }
  157. ],
  158. "distance": 0.6028837702084446
  159. }
  160. ]

I have no idea how to make proper assertions, any help would be great. Thanks!

答案1

得分: 2

如果我理解您的问题正确,您需要检查特定值是否存在于返回给定ID的列表中。

以下内容适用于您:

  1. given().when().get().then().body("find {it.id == 346}.personDependencies.name", hasItems("json", "jackson"));
英文:

If I am reading your question right you need to check if a certain values are present in a list that is returned for a particular ID

The below should work for you

  1. given().when().get().then().body("find {it.id == 346}.personDependencies.name", hasItems("json", "jackson"));

答案2

得分: 1

第一个问题是你不需要使用hasItems来检查项目的存在,应该使用hasItem

  1. when().
  2. get("/api/personsByID/{id}/{count}", 262, 2).
  3. then().
  4. statusCode(200).
  5. body("personDependencies.name", hasItem("json"));

然后,如果在测试失败时需要向断言添加更多消息,你可以这样做:

  1. when().
  2. get("/api/personsByID/{id}/{count}", 262, 2).
  3. then().
  4. statusCode(200).
  5. body("personDependencies.name", describedAs("数组不包含所提供的项", hasItem("json")));

在你的情况下,你可以这样验证:

  1. when().
  2. get("/api/personsByID/{id}/{count}", 262, 2).
  3. then().
  4. statusCode(200).
  5. body("personDependencies[*].name", hasItem("json"));
英文:

The first problem you don't need to check the presence of an item with hasItems, you should use hasItem

  1. when().
  2. get("/api/personsByID/{id}/{count}", 262, 2).
  3. then().
  4. statusCode(200).
  5. body("personDependencies.name", hasItem("json"));

Then if you need to add more message to the assertion when the test fails you can do such way:

  1. when().
  2. get("/api/personsByID/{id}/{count}", 262, 2).
  3. then().
  4. statusCode(200).
  5. body("personDependencies.name", describedAs("Array not containing the provided item",hasItem("json")));

In your case you can validate such a way:

  1. when().
  2. get("/api/personsByID/{id}/{count}", 262, 2).
  3. then().
  4. statusCode(200).
  5. body("personDependencies[*].name", hasItem("json"));

答案3

得分: 0

Wilfred Clement的回答非常好。只想再添加一个例子以更好地理解概念和语法。
如果您必须在某个不在JSON的'root'级别上的数组中进行类似的搜索,请使用以下语法:

  1. .body("problems.find {it.property == 'boardUrl'}.details", is("size must be between 1 and 256"))

此路径适用于以下JSON结构:

  1. {
  2. "title": "Bad Request",
  3. "status": 400,
  4. "problems": [
  5. {
  6. "property": "tag",
  7. "value": "too-big-tag-value",
  8. "details": "size must be between 1 and 10"
  9. },
  10. {
  11. "property": "name",
  12. "value": "Very big name for testing purposes",
  13. "details": "size must be between 1 and 20"
  14. }
  15. ]
  16. }
英文:

Wilfred Clement's answer is great. Just want to add 1 more example for better understanding concept and syntax.
Use next syntax if you have to make similar search in some array which is not on 'root' level of JSON:

  1. .body("problems.find {it.property == 'boardUrl'}.details", is("size must be between 1 and 256"))

This path is created for the JSON structure below:

  1. {
  2. "title": "Bad Request",
  3. "status": 400,
  4. "problems": [
  5. {
  6. "property": "tag",
  7. "value": "too-big-tag-value",
  8. "details": "size must be between 1 and 10"
  9. },
  10. {
  11. "property": "name",
  12. "value": "Very big name for testing purposes",
  13. "details": "size must be between 1 and 20"
  14. }
  15. ]
  16. }

huangapple
  • 本文由 发表于 2020年7月27日 19:05:42
  • 转载请务必保留本文链接:https://java.coder-hub.com/63114006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定