unit-testingtestingassertiongoogle-truth

How to check String equality in Google Truth assertions?


Truth.assertThat(actual).matches(expected) or Truth.assertThat(actual).isEqualTo(expected) ?

The docs say that the matches() method takes in a String in the form of a regex but not sure if a string literal works as well? That's what got me confused.


Solution

  • It sounds like you want isEqualTo(expected), which performs an exact equality assertion.

    As you say, matches accepts a regex, which lets you do things like assertThat("foo").matches("f.*"). But regexes can interfere with exact matching. For example, assertThat("$5").matches("$5") will fail because the $ in the regex means "end of string." But assertThat("$5").isEqualTo("$5") will pass.