Consider assertThat() in place of assertEquals()
JUnit 4.4 added a new assertion mechanism with the method assertThat(). Have a look and consider using it in place of assertEquals().
assertThat(result, is(42));
assertThat(output, containsString("foo"));
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)
Tags:






Comments
Jeroen Van Dijk replied on Wed, 2012/10/10 - 7:09am
Why? What's the advantage?
For what I see now is that it's more readable for normal people. But only programmers write those tests, and they understand the 'old' way without any problem...
Although I AM in favor of writing readable code, assertEquals("this is no good", thisOne, thatOne)is readable imho, even more, when we're talking about assertThat(result, is(true)) versus assertTrue(result)...
Martin Tilma replied on Wed, 2012/10/10 - 8:19am
Tomasz Nurkiewicz replied on Wed, 2012/10/10 - 8:32am
Even better, use FEST assertions:
assertThat(result1).isEqualTo(42);
assertThat(result2).contains("foo");
Note that your example is a bit confusing. You are checking whether "result" variable is equal to 42 and contains "foo" string - at the same time.
Jeroen Van Dijk replied on Wed, 2012/10/10 - 9:12am
in response to:
Martin Tilma
David Karr replied on Wed, 2012/10/10 - 11:33am