断言 Set 包含具有 return 给定值的方法的对象

Asserting that a Set contains objects with methods that return given values

Class A 有一个方法 getId(),它 return 是一个字符串。

Class B 有一个方法 getCollection(),它 return 是一个集合(顺序未定义)

我希望我的测试验证 returned 集合包含 A 的实例,每个实例 return getId()

的预期值
public interface A {
    String getId ();
}

public interface B {
    Collection<A> getCollection ();
}

public class BTest {

    @Test
    public void test () {
        B b = ( ... )
        Collection<A> collection = b.getCollection();
        assertEquals(3, collection.size());

        String expId1 = "id1";
        String expId2 = "id2";
        String expId3 = "id3";

        // There should be 3 A's in this collection, each returning
        // one of the expected values in their getId()
    }

}

我只能想到这里真的不优雅的东西。我目前正在使用 JUnit/Hamcrest/Mockito。如果最好的解决方案是图书馆,那不是问题

Java-8解,够优雅吗?

public class BTest {

    @Test
    public void test () {
        B b = ( ... )
        Set<String> expectIds = new HashSet<>(Arrays.asList("id1","id2","id3"));
        Collection<A> collection = b.getCollection();
        Set<String> ids = collection.stream().map(a->a.getId()).collect(Collectors.toSet());

        assertEquals(3, collection.size());
        assertEquals(expectIds, ids);

    }

}

已编辑:

断言J:http://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html

public class BTest {

    @Test
    public void test () {
        B b = ( ... )
        Collection<A> collection = b.getCollection();

        assertEquals(3, collection.size());
        assertThat(collection).extracting("id").contains("id1","id2","id3");
    }
}

使用collection.contains(对象o)方法 但是你应该覆盖 A class 的 equals() 和 hashCode() 以获得 Objects

的预期结果

您可以使用 Mockito 模拟接口 A。

@Test
public void test () {
    //given: we have our expected and actual lists.
    List<String> expectedResult = Arrays.asList("id1","id2","id3");

    //build our actual list of mocked interface A objects.
    A a1 = mock(A.class);
    when(a1.getId()).thenReturn("id1");
    A a2 = mock(A.class);
    when(a2.getId()).thenReturn("id2");
    A a3 = mock(A.class);
    when(a3.getId()).thenReturn("id3");
    B b = mock(B.class);
    Collection<A> actualResult = Arrays.asList(a1, a2,a3);

    //when: we invoke the method we want to test.
    when(b.getCollection()).thenReturn(actualResult);

    //then: we should have the result we want.
    assertNotNull(actualResult);
    assertEquals(3, actualResult.size());
    for (A a : actualResult) assertTrue(expectedResult.contains(a.getId()));

}

您可以使用 Hamcrest 的 containsInAnyOrderhasProperty 匹配器。

@Test
public void test () {
    B b = ( ... )
    Collection<A> collection = b.getCollection();
    assertThat(collection, containsInAnyOrder(
        Matchers.<A>hasProperty("id", equalTo("id1")),
        Matchers.<A>hasProperty("id", equalTo("id2")),
        Matchers.<A>hasProperty("id", equalTo("id3"))));
}

这会在失败的情况下打印一条信息性消息。例如。如果集合有第四个元素机智 id id4:

Expected: iterable over [hasProperty("id", "id1"), hasProperty("id", "id2"), hasProperty("id", "id3")] in any order
     but: Not matched: <A{id='id4'}>