Java Builder Pattern for Testers

Java builder pattern for testers too

The java builder pattern is an alternative way of constructing objects. The java builder pattern is useful for testers too.

Suppose you have to send some person data to our backend. That person has a name. In our tests you create a Person class with a constructor that has the persons name as parameter.

public class Person {
    private String name;
    public Person(String name){
        this.name = name;
    }
};

Now you can create the Person and send it to the back-end. Now suppose that the Person class needs more data. For example address, city, family name and first name. The constructor has many parameters now.

public class Person {
    private String name;
    private String surname;
    private String address;
    private String city;
    private String country;
    private String birthDate;
   
    public Person(String name, String surname, 
                  String address, String city, 
                  String country, String birthDate){
        this.name = name;
        this.surname = surname;
        this.address = address;
        this.city = city;
        this.country = country;
        this.birthDate = birthDate;
    }
}

When you create an instance, the constructor needs a lot of parameters.

Person person = new Person("Anne", "Bonny", "Unknown", "Cork", "Ireland", "1697-03-08");

These constructors needs a lot of parameters. If you only want to test for some birth dates, you need to pass in all other fields too. If the person class gets bigger, the impact in our tests is huge. The constructor needs an update for each test. There should be a better way.

Lets try it with creating some setters.

public class Person {
    private String name;
    private String surname;
    private String address;
    private String city;
    private String country;
    private String birthDate;
   
    public Person(){
        this.name = "Anne";
        this.surname = "Bonny";
        this.address = "Unknown";
        this.city = "Cork";
        this.country = "Ireland";
        this.birthDate = "1697-03-08";
    }

    public void setName(String name){this.name = name;}
    public void setSurName(String surname){this.surname = surname;}
    public void setAddress(String address){this.address = address;}
    public void setCity(String city){this.city = city;}
    public void setCountry(String country){this.country = country;}
    public void setBirthDate(String birthDate){this.birthdate = birthDate;}
}

This pattern has an easy constructor with no parameters. The constructor fills in some default parameters. During construction default parameters are filled in. If you want to test for different birth dates, you call the setBirthDate function.

Person person = new Person();
person.setBirthDate("2020-03-08");

The Java Builder pattern

There is another way to create this Person with only one line of code. In that case we need the builder pattern.

public class Person {
    private final String name;
    private final String surname;
    private final String address;
    private final String city;
    private final String country;
    private final String birthDate;

    public static class Builder {
        private String name = "Anne";
        private String surname = "Bonny";
        private String address = "Unknown";
        private String city = "Cork";
        private String country = "Ireland";
        private String birthDate = "1697-03-08";
       
        public Builder(){}
        public Builder name(String value){
            name = value; 
            return this;
        }
        public Builder surname (String value){
            surname = value; 
            return this;
        }
        public Builder address (String value){
            address = value; 
            return this;
        }
        public Builder city (String value){
            city = value; 
            return this;
        }
        public Builder country (String value){
            country = value; 
            return this;
        }
        public Builder birthDate (String value){
            birthDate = value; 
            return this;
        }
        public Person build(){
            return new Person(this);
        }
    }
 
    public Person(Builder builder){
        this.name = builder.name
        this.surname = builder.surname;
        this.address = builder.address;
        this.city = builder.city;
        this.country = builder.country;
        this.birthDate = builder.birthDate;
    }
}

The Person class has now a builder that builds Person objects. Each method returns the builder itself. You can chain the invocations too. The client code can look now like this:

Person person = new Person.Builder().birthDate("2020-03-08").country("Germany").build();

The caller can create an object with only one line of code. The caller of the Person class do not need to update the code when the Person class contains extra fields. That is an extra advantage of this java builder pattern.

When a class has many optional parameters, the java builder pattern is a good choice for testers too. The code is easier to read and that is a big advantage.

About the author

I currently work as a Test Automation Consultant at b.ignited. Here I work for different clients in different industries to help them start and speed up their testing cycles

I’ve been testing software since 2000 when I became involved in testing telephone applications and hardware. Since then, I’ve been able to expand my experience by testing a variety of embedded, web, mobile and desktop applications. I have used various development methodologies from waterfall to agile.

I consider myself to be a lifelong learner.

Comments

Comments are closed.