Custom Asserts For ModelAndView

This blog posts present some custom assertions written for Spring MVC ModelAndView class to make the tests more readable.

Some time ago I fell in love with all kind of DSLs including builders and custom asserts. Recently when browsing through some tests I discovered few dozens of lines like this:

assertThat(mav.getModelMap().get("transaction")).isEqualTo(pt);

where mav is of the ModelAndView class.
Because I'm alergic to so-called "train wrecks" (x.y.get() etc.) I scratched my head wondering if I can do something about it. The number of such assertions legitimates (at least in my opinion) some effort in order to improve them.

And so I spent 30 minutes of my lifetime hacking a custom assert (using FEST Fluent Assertions) which would allow to make these assertions a little bit more readable. The code is on github, see https://github.com/tomekkaczanowski/varia/tree/master/custom_fest_assert... And yes, you can use it in your code and I won't sue you, I promise.

Basically what it allows is to change this:

assertThat(mav.getModelMap().get("transaction")).isEqualTo(pt);

into this:

assertThat(mav).contains("transaction", pt);

(see Examples on github - there is some more stuff there)

If you do not like it you could easily change it into something like:

assertThat(mav).contains(pt).underKey("transaction");

or whatever your taste dictates you.

Happy coding!

 
 
 
This used to be my blog. I moved to http://tomek.kaczanowscy.pl long time ago.

 
 
 

Please comment using