Java 检查JSON响应中的每个对象是否在特定字段中包含短语

Java 检查JSON响应中的每个对象是否在特定字段中包含短语,java,json,junit,jsonpath,hamcrest,Java,Json,Junit,Jsonpath,Hamcrest,今天,我需要为公共API使用者创建一个集成测试,并检查我找到的记录是否与查询参数匹配。 例如,我正在发送一个GET请求,如 localhost:8080/nutritionix/productDetails?query=grilled 我想找出每一种产品都包含“grilled”或“grilled”或其他形式的相同短语,例如“food_name”字段中的“grilled”短语 来自公共API的响应示例: [ { "food_name": "chicken grilled

今天,我需要为公共API使用者创建一个集成测试,并检查我找到的记录是否与查询参数匹配。
例如,我正在发送一个GET请求,如

localhost:8080/nutritionix/productDetails?query=grilled 
我想找出每一种产品都包含“grilled”或“grilled”或其他形式的相同短语,例如“food_name”字段中的“grilled”短语

来自公共API的响应示例:

[
    {
        "food_name": "chicken grilled",
        "serving_qty": 1,
        "serving_unit": "piece",
        "photo": {
            "thumb": "https://d2xdmhkmkbyw75.cloudfront.net/1714_thumb.jpg"
        }
    },
    {
        "food_name": "grilled chicken thigh",
        "serving_qty": 1,
        "serving_unit": "thigh with skin",
        "photo": {
            "thumb": "https://d2xdmhkmkbyw75.cloudfront.net/8724_thumb.jpg"
        }
    },
    {
        "food_name": "grilled chicken wrap",
        "serving_qty": 1,
        "serving_unit": "wrap",
        "photo": {
            "thumb": "https://d2xdmhkmkbyw75.cloudfront.net/2562_thumb.jpg"
        }
    },
    {
        "food_name": "grilled cheese",
        "serving_qty": 1,
        "serving_unit": "sandwich",
        "photo": {
            "thumb": "https://d2xdmhkmkbyw75.cloudfront.net/1763_thumb.jpg"
        }
    },
    {
        "food_name": "Grilled Tilapia, Signature Grilled",
        "serving_qty": 1,
        "serving_unit": "fillet",
        "brand_name": "Gorton's",
        "nix_brand_id": "51db37b2176fe9790a8985bc",
        "photo": {
            "thumb": "https://d1r9wva3zcpswd.cloudfront.net/55178d395108f25f51667c2d.jpeg"
        }
    },
    {
        "food_name": "Grilled Gourmet Soft Taco, Grilled Chicken",
        "serving_qty": 189,
        "serving_unit": "g",
        "brand_name": "Amigos Kings Classic",
        "nix_brand_id": "521b95434a56d006cae297dc",
        "photo": {
            "thumb": "https://d2eawub7utcl6.cloudfront.net/images/nix-apple-grey.png"
        }
    }
]
测试等级如下:

@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
@SpringBootTest
class NutritionixApiControllerTest {

  @Autowired
  private MockMvc mockMvc;

  @Test
  void nutritionixSearchInstantTest() throws Exception {

    ResultActions resultActions = mockMvc
        .perform(MockMvcRequestBuilders.get("/nutritionix/productDetails?query=grilled")
            .contentType(MediaType.APPLICATION_JSON)).andDo(print())
        .andExpect(MockMvcResultMatchers.status().isOk())
        .andExpect(MockMvcResultMatchers.jsonPath("$", hasSize(40)))
        .andExpect(MockMvcResultMatchers.jsonPath("$[*].food_name", hasItems("grilled")))
        .andExpect(MockMvcResultMatchers.jsonPath("$[0].food_name", containsString("grilled")));

  }
不幸的是,这一行:

.andExpect(MockMvcResultMatchers.jsonPath("$[*].food_name", hasItems("grilled")))

导致断言错误,如:

java.lang.AssertionError: JSON path "$[*].food_name"
Expected: (a collection containing "grilled")
     but: a collection containing "grilled" was "grilled chicken thigh", was "Fiesta Grilled Chicken", was "grilled zucchini", was "grilled octopus", was "grilled veggies", was "Grilled Chicken Pesto with Vegetables", was "Grilled Chicken Salad", was "grilled cheese", was "Grilled Chicken Strips", was "Grilled Chicken for Grilled Chicken Salad", was "grilled chicken", was "chicken grilled", was "Grilled Chicken Marinara, with Parmesan", was "Greek Dressing for Grilled Topped Salads", was "chilis grilled chicken salad", was "Caesar Dressing for Grilled Topped Salads", was "grilled onions", was "Grilled & Ready Grilled Chicken Breast Chunks", was "Grilled Cali Bowl, Grilled", was "Mesquite Grilled Chicken Fillets", was "grilled chicken salad", was "Grilled Chicken Marsala", was "Grilled Gourmet Soft Taco, Grilled Chicken", was "Grilled Chicken & Broccoli Alfredo", was "grilled salmon", was "Fire-Grilled Grilled Steak Salad", was "Grilled Tilapia, Signature Grilled", was "Grilled Salmon, Classic Grilled", was "Grilled Basil Chicken", was "grilled steak", was "grilled chicken wrap", was "grilled chicken sandwich", was "Big Sky Grilled Salad (grilled chicken)", was "grilled fish", was "Grilled Roll-Up, Grilled Chicken", was "grilled shrimp", was "grilled hamburger", was "grilled chicken breast", was "grilled vegetables", was "grilled asparagus"

我想要实现的就是检查我在JSON响应中收到的所有对象是否在food_name字段中包含短语,我们在查询期间将其作为查询参数传递,包括小写和大写字母以及有趣的文本大小写

我认为问题的出现是因为返回的记录模式缺乏标准化,例如:有时我们得到带有food_name字段的对象,它们是大写的,有时是小写的


感谢您对如何改进JSONPath语法的帮助

问题在于,您正在检查结果的任何元素是否等于
grilled
。我认为您需要检查的是,结果的每个元素是否都包含格状忽略

使用库(我认为您已经在使用它)可以通过以下方式进行:

@Test
public void everyContainsStringIgnoringCase() {
    List<String> foodNames = JsonPath.read(RESPONSE, "$[*].food_name");
    assertThat(foodNames, Every.everyItem(containsStringIgnoringCase("grilled")));
}
@测试
public void everycontainsstringnoringcase(){
List foodNames=JsonPath.read(响应,“$[*].food_name”);
资产(食品名称,每件每件物品(包含“铁扒”));
}

您的回答是json。

您的回答完全解决了一个问题。汉克雷斯特的依赖性有问题,但毕竟,我达到了一个目标。非常感谢你的回答。