🇺🇸 Builder Pattern applied to Unit Testing

Arley Pádua
2 min readNov 16, 2017

When writing a sofware, you as a software developer, must write something to ensure the quality of your code. If we are dealing with test driven development, it is a good practice having your unit tests well implemented, because in the future, as the software evolves, you will have to look into your tests, understand them, modify them to acomplish your job.

It is very common make an special effort to write your unit tests in a good readable way.

In addition to this effort, following a suggestion from Steve Smith, I implemented the builder pattern applied with unit testing to see the results. With a combination of: Microsoft Unit Test, C#, FluentAssertions and the builder pattern, I could see a great test in a simple scenario and would like to share with y’all.

First I created a simple class named Employee with the properties: name, birth date and gross salary. Additionally I added a behavior to calculate the net salary.

The second step is to implement your Employee builder:

And finally just write your unit tests tying those methods together and assert the expected result at your will:

Afterall, you will have a good readable code, without much additional effort.

To see the complete code, use the following git repository.

This article is also available in Portuguese.

--

--

Arley Pádua
Arley Pádua

Written by Arley Pádua

Software Engineer and passionate about distributed systems

Responses (4)