使用 jsonPath 测试值列表中包含的重复值 (Hamcrest)

Testing with jsonPath for repeating values which are contained in a list of value (Hamcrest)

我有以下 json 结构:

{
  content: [
    {
      status: 100,
    },
    {
      status: 100,
    },
    {
      status: 200,
    },
    {
      status: 300
    }
  ]
}

我在 Spring 中使用以下代码来测试 json 中的任何状态值是否包含状态数组指定的可接受值:

jsonPath("$.content[*].status", Matchers.containsInAnyOrder(100, 200, 300));

它失败了,因为检查的可迭代对象不是,其长度与指定项的数量相同。如果我像这样指定数组 Matchers.containsInAnyOrder(100, 100, 200, 300),那么它就成功了。

我检查了匹配器的实现,看起来每次成功匹配时,匹配值都会从指定项中删除。

有没有不会删除匹配项的 Hamcrest 匹配器?

Matchers.containsInAnyOrder

Creates an order agnostic matcher for Iterables that matches when a single pass over the examined Iterable yields a series of items, each logically equal to one item anywhere in the specified items.

但你真正想做的是检查 每个 项目是否在 一组有效的值,可以这样写:

Matchers.everyItem(Matchers.isOneOf(100,200,300))