0
0
JUnittesting~15 mins

Builder pattern for test data in JUnit - Build an Automation Script

Choose your learning style9 modes available
Create and verify a User object using Builder pattern
Preconditions (2)
Step 1: Use the User.Builder to create a User with name 'Alice', age 30, and email 'alice@example.com'
Step 2: Verify that the User object has name 'Alice'
Step 3: Verify that the User object has age 30
Step 4: Verify that the User object has email 'alice@example.com'
✅ Expected Result: User object is created correctly with the specified attributes
Automation Requirements - JUnit 5
Assertions Needed:
assertEquals for name
assertEquals for age
assertEquals for email
Best Practices:
Use Builder pattern to create test data
Use clear and descriptive assertion messages
Keep test method focused on one scenario
Automated Solution
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

public class UserTest {

    @Test
    void testUserBuilderCreatesCorrectUser() {
        User user = new User.Builder()
                .name("Alice")
                .age(30)
                .email("alice@example.com")
                .build();

        assertEquals("Alice", user.getName(), "User name should be Alice");
        assertEquals(30, user.getAge(), "User age should be 30");
        assertEquals("alice@example.com", user.getEmail(), "User email should be alice@example.com");
    }
}

// User class with Builder pattern
class User {
    private final String name;
    private final int age;
    private final String email;

    private User(Builder builder) {
        this.name = builder.name;
        this.age = builder.age;
        this.email = builder.email;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getEmail() {
        return email;
    }

    public static class Builder {
        private String name;
        private int age;
        private String email;

        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public Builder age(int age) {
            this.age = age;
            return this;
        }

        public Builder email(String email) {
            this.email = email;
            return this;
        }

        public User build() {
            return new User(this);
        }
    }
}

This test uses the Builder pattern to create a User object with specific attributes.

The User.Builder class allows setting each attribute step-by-step, making the test data creation clear and flexible.

Assertions check that the User object has the expected name, age, and email.

Each assertion includes a message to help understand failures.

This approach keeps the test clean and focused on verifying the builder functionality.

Common Mistakes - 3 Pitfalls
Creating User object directly without using Builder
Not asserting all fields set by the Builder
Using hardcoded strings in multiple places without variables
Bonus Challenge

Now add data-driven testing with 3 different User inputs using JUnit 5 parameterized tests

Show Hint