0

How can I write the following assertion:

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

with Google Truth assertThat?

com.google.common.truth.Truth.assertThat(result...
JJD
  • 50,076
  • 60
  • 203
  • 339

1 Answers1

2

Provided that I'm not familiar with Google Truth (hence I don't know if there's an idiomatic way to write it), I would write that assertion like this:

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

or, you can keep the original version:

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

Playing a bit with it, you could even do:

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

However, the latter doesn't look very readable, so I'd stick with the first one.

Chris Povirk
  • 3,738
  • 3
  • 29
  • 47
user2340612
  • 10,053
  • 4
  • 41
  • 66