There are 2 methods

assertThat(EmailValidator.isValidEmail("name@email.com"), is(true)); 

and

 assertTrue(EmailValidator.isValidEmail("name@email.com")); 

What is their difference? I understand they do the same thing ...

When to use one and when another?

  • The fact that it is you say this way. In the first case, you have an abstract statement, and the framework will display a text message “statement failed”, in the second case, it will simply say “false was not true, as expected”. The difference is really quite subtle. - etki
  • @Etki I see that in the case of assertThat more intelligible information is written in the log ... It’s better to use this method where I can use it ... - Aleksey Timoshchenko
  • Yes, assertTrue is needed in cases where there is no time to sign tests and when some boolean method is checked (eg payload.isValid ()) - etki
  • @Etki you wrote that when there is no time, then ideally you should still strive to use assertTrue(), assertFalse() и т.д. ? - Aleksey Timoshchenko
  • No, I just recall that example with assert X is instance of Y. There is nothing wrong with calling assertTrue(x instanceof Y) , which will save time on a small project, but with proper writing of tests there should be assertThat(x, <нужный матчер>) - etki

1 answer 1

Using assertTrue , you say that you are comparing a variable with true . And assertThat you can compare not only with true . for example, here is assertThat(x, is(3)) - you check that x is 3. And here, assertTrue , is not applicable, respectively

  • I understand that such an approach is more flexible or something ... But in the case of numbers, we can also use assertEquals() ... it turns out assertThat() replaces all this variety with true, false, equals one method - Aleksey Timoshchenko nov
  • Here is a link to the junit.sourceforge.net/doc/ReleaseNotes4.4.html documentation. there are described the advantages of such assertThat() - pavel163