Easily create builders for your tests using Intellij IDEA

The builder pattern is one of the more useful patterns out there when creation unit tests.

Instead of having a huge initialization such as:

@Test
void validateUser_userNameIsEmpty_returnFalse(){
    var user = new User();
    user.setId("id-1");
    user.setName(null);
    user.setPhoneNumber("555-1234");
    // additional user initialization

   boolean result = userService.validate(user);

   assertFalse(result);
}

You can instead create a simple user builder object for your tests that would initialize all of the object’s properties with default values (not empty) – since you do not really care what the values are as long as they are valid and write the following test:

@Test
void validateUser_userNameIsEmpty_returnFalse(){
    var user = new UserBuilder()
                      .withName(null)
                      .build();

   boolean result = userService.validate(user);

   assertFalse(result);
}

It helps you reduce the test code avoid duplication and if the user’s class changes in some way you do not need to fix 100+ tests. But the real benefit is that using builders in your tests is that they help to focus the test reader on what’s important in this test. This is very important because the test reader might be you trying to understand why a test you wrote three months ago started failing at 8pm on the day before a major release.

If you want to learn more about object initialization you can check my post on the subject: "On object creation and unit tests"

The problem with builders

But it’s not all rainbows and sunshine when creation builders for your tests, it is also a painful experience, you need to create a new class, then add all of the values you need to set in that object, implement setter methods and build method and it’s not a fun experience which has caused me more often than not to avoid creationg builders until my test code has become too painful to maintain.

But that was in the past, since I found a cool new feature in Intellij, I found out that when I create setters for a class using code generation I can choose those setters to use the builder template.

Now creating builders for my tests is easy:

  1. Create a new builder class
  2. Copy the fields you need from the original class
  3. Set default values for all fields
  4. Create a build function using a constructor – or if you must setter methods
  5. Generate all of the setter methods as build methods with a click of your keyboard (alt+insert)
  6. Write tests using your new and shiny class.

Quick and simple, and relatively painless.

If you’re a .NET developer (I know I am) you might be wondering if the same feature exist in R# and/or Visual Studio (I know I did). Unfortunately it does not I guess it’s because it was never requested and properties in .NET are implemented differently. I guess you’ll have to ask JetBrains to add this feature or create your own code template.

And until Then… Stay healthy and happy coding…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.