在Hamcrest中进行测试,该列表仅存在具有特定属性的列表中的一个项目 [英] Testing in Hamcrest that exists only one item in a list with a specific property

查看:72
本文介绍了在Hamcrest中进行测试,该列表仅存在具有特定属性的列表中的一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Hamcrest,我们可以轻松测试具有特定属性的列表中存在至少一个项目,例如

With Hamcrest we can easily test that exists at least one item in a list with a specific property, e.g.

List<Pojo> myList = ....

MatcherAssert.assertThat(myList, Matchers.hasItem(Matchers.<Pojo>hasProperty("fieldName", Matchers.equalTo("A funny string")))));

其中类 Pojo 类似于:

public class Pojo{
  private String fieldName;
}

这很好,但我怎么能检查出来中只有一个对象具有特定属性的列表?

That's nice, but how can I check that there is exactly one object in the list with the specificed properties?

推荐答案

您可能必须为此编写自己的匹配器。 (我更喜欢fest断言和Mockito,但过去习惯使用Hamcrest ......)

You might have to write your own matcher for this. (I prefer the fest assertions and Mockito, but used to use Hamcrest...)

例如......

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.core.IsCollectionContaining;

public final class CustomMatchers {

    public static <T> Matcher<Iterable<? super T>> exactlyNItems(final int n, Matcher<? super T> elementMatcher) {
        return new IsCollectionContaining<T>(elementMatcher) {
            @Override
            protected boolean matchesSafely(Iterable<? super T> collection, Description mismatchDescription) {
                int count = 0;
                boolean isPastFirst = false;

                for (Object item : collection) {

                    if (elementMatcher.matches(item)) {
                        count++;
                    }
                    if (isPastFirst) {
                        mismatchDescription.appendText(", ");
                    }
                    elementMatcher.describeMismatch(item, mismatchDescription);
                    isPastFirst = true;
                }

                if (count != n) {
                    mismatchDescription.appendText(". Expected exactly " + n + " but got " + count);
                }
                return count == n;
            }
        };
    }
}

您现在可以做...

    List<TestClass> list = Arrays.asList(new TestClass("Hello"), new TestClass("World"), new TestClass("Hello"));

    assertThat(list, CustomMatchers.exactlyNItems(2, hasProperty("s", equalTo("Hello"))));

列表为...时的失败输出示例

Example fail output when the list is...

    List<TestClass> list = Arrays.asList(new TestClass("Hello"), new TestClass("World"));

...将... ...

...will be...

Exception in thread "main" java.lang.AssertionError: 
Expected: a collection containing hasProperty("s", "Hello")
     but: , property 's' was "World". Expected exactly 2 but got 1

(你可能想稍微调整一下)

(You might want to customise this a bit)

顺便说一句,TestClass是......

By the way, "TestClass" is...

public static class TestClass {
    String s;

    public TestClass(String s) {
        this.s = s;
    }

    public String getS() {
        return s;
    }
}

这篇关于在Hamcrest中进行测试,该列表仅存在具有特定属性的列表中的一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆