junit 如何将复杂对象列表中的成员与Hamcrest进行比较?

lf3rwulv  于 2022-11-11  发布在  其他
关注(0)|答案(3)|浏览(154)

假设我有一个List<A>

class A {
    private Integer val;
    private String name;
}

在我的测试用例中,我得到了这个列表,它的大小和内容都不确定,我想做的是将我知道必须在列表中的两个列表元素的val字段与给定的name字段进行比较;

List<A> list = logic.getList();
assertThat(list, allOf(hasItems(hasProperty("name", equalTo("first")), 
                       hasItems(hasProperty("val", equalTo(***value from another member with name = "second"))));

我如何才能做到这一点,或者这是甚至可能与Hamcrest匹配?

jvidinwx

jvidinwx1#

您可以根据自己的需要实现自定义Matcher,例如,检查某些具有名称的项是否具有相同值字段:

final class FooTest {

    static final class Foo {

        final int val;
        final String name;

        // all args constructor
    }

    // custom matcher
    static final class FoosHasSameValues extends TypeSafeMatcher<List<Foo>> {

        private final Set<String> names;

        // all args constructor

        FoosHasSameValues(final String... names) {
            this(new HashSet<>(Arrays.asList(names)));
        }

        @Override
        protected boolean matchesSafely(final List<Foo> items) {
            final List<Integer> values = items.stream()
                // filter only items with specified names
                .filter(i -> this.names.contains(i.name))
                // select only values
                .map(i -> i.val)
                .collect(Collectors.toList());
            if (values.size() != this.names.size()) {
                // matching failed if list doesn't contains all
                // needed items with names
                return false;
            }
            // check https://stackoverflow.com/a/29288616/1723695
            return values.stream().distinct().limit(2).count() <= 1;
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("has items [")
                .appendValue(String.join(", ", this.names))
                .appendText("] with same values");
        }
    }

    @Test
    void testMatchers() throws Exception {
        MatcherAssert.assertThat(
            Arrays.asList(
                new Foo("first", 1),
                new Foo("second", 1),
                new Foo("third", 2)
            ),
            new FoosHasSameValues("first", "second")
        );
    }
}
laawzig2

laawzig22#

编写自定义匹配器可以清理测试逻辑:

public class AMatcher extends TypeSafeMatcher<A> {
   A actual;
   public AMatcher(A actual) { this.actual = actual; }

   protected boolean matchesSafely(A a) {
      return a.equals(actual);  // or compare individual fields...
   } 

   public void describeTo(Description d) {
      d.appendText("should match "+actual); // printed out when a match isn't found.
   }
}

然后,使用它:

assertThat(list, allOf(new AMatcher(a1), new AMatcher(a2)));

或者,如果您不想创建A的示例来创建匹配器,请创建一个AMatcher构造函数,该构造函数接受您的“name”和“瓦尔”参数。

hyrbngr7

hyrbngr73#

不得不这么做。
用途:
(Java集合)
确保,类A重写对象等于方法,比较瓦尔和name。

class A {
    private Integer val;
    private String name;
}

相关问题