如何在任何条件下使用 assertThat?

How to use assertThat with any condition?

如何编写以下断言:

org.junit.Assert.assertTrue(result.any { it.name == "Foo" })

与Google真相assertThat?

com.google.common.truth.Truth.assertThat(result...

如果我不熟悉 Google 真相(因此我不知道是否有惯用的方式来写它),我会这样写断言:

Truth.assertThat(result.map { it.name }).contains("foo")

或者,您可以保留原始版本:

Truth.assertThat(result.any { it.name == "foo" }).isTrue()

玩一下,你甚至可以做到:

Truth.assertThat(result)
        .comparingElementsUsing(Correspondence.transforming<Foo, String>({ foo -> foo?.name }, "has the same name as"))
        .contains("foo")

然而,后者看起来不太可读,所以我还是坚持第一个。