09 July 2014

To build clean and expressive unit tests I have been using the Builder pattern lately. I like to keep the tests clean and readable and this is exactly what the Builder pattern allows me to do.

The Product class below has a couple of attributes that get set via setters and one calculated value based on the values of the other attributes.

The ProductBuilder class builds products using default values and exposes with* methods to set individual product attributes.

The unit test below has two tests, one with the default values as defined in the builder class and one with (some) values set using the with* methods.



blog comments powered by Disqus