Comments

05/28/05: Flexible JUnit assertions with assertThat()

Joe Walnes describes a Clever junit base class with declarative methods for doing stuff like this:

assertThat(something, eq("Hello"));
assertThat(something, eq(true));
assertThat(something, isA(Color.class));
assertThat(something, contains("World"));
assertThat(something, same(Food.CHEESE));
assertThat(something, NULL);
assertThat(something, NOT_NULL);
assertThat(something, not(eq("Hello")));
assertThat(something, not(contains("Cheese")));
assertThat(something, or(contains("color"), contains("colour")));

And you get failure description messages for free! -- it infers them from the constraints.

I like code you can read.


Comments made

So with this assertion:

assertThat(something, or(contains("color"), contains("colour")));

I get this useful failure message, for free:

junit.framework.AssertionFailedError:
Expected: (a string containing "color" or a string containing "colour")
but got : hello world
05/31/05 15:44:04
For any internet user, if one keys in the phrase Oakley sunglasses,he many get thousands of results, of which, items like cheap sunglasses, cheap designer sunglasses and http://www.oakleysglassesfa... sunglasses are very striking.
02/28/12 23:53:24

Add comment

Sorry, but due to blog comment spam, I have to ask you to create an account before you post a comment. Please log in (using the form on the top right of the page) or click here to create an account: Create an account!