How to check String equality in Google Truth assertions?

1k views Asked by At

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.

1

There are 1 answers

2
Chris Povirk On BEST ANSWER

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.