0
0
JUnittesting~20 mins

Builder pattern for test data in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Builder Pattern Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of JUnit test using Builder pattern
Given the following JUnit test code using a Builder pattern for creating test data, what will be the test result?
JUnit
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class User {
    private String name;
    private int age;

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

    public String getName() { return name; }
    public int getAge() { return age; }

    public static class Builder {
        private String name = "Default";
        private int age = 18;

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

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

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

public class UserTest {
    @Test
    public void testUserBuilder() {
        User user = new User.Builder()
            .name("Alice")
            .age(25)
            .build();

        assertEquals("Alice", user.getName());
        assertEquals(25, user.getAge());
    }
}
ATest fails because age is 18, not 25
BCompilation error due to missing Builder constructor
CTest fails because name is "Default", not "Alice"
DTest passes successfully
Attempts:
2 left
💡 Hint
Check how the Builder sets the fields before building the User object.
assertion
intermediate
1:30remaining
Correct assertion for Builder-created object
You have a User object created by the Builder with name "Bob" and age 30. Which assertion correctly verifies the age in JUnit 5?
JUnit
User user = new User.Builder().name("Bob").age(30).build();
AassertEquals(30, user.getAge());
BassertTrue(user.getAge() == 30);
CassertEquals("30", user.getAge());
DassertFalse(user.getAge() != 30);
Attempts:
2 left
💡 Hint
Use the assertion that compares expected and actual values with correct types.
🔧 Debug
advanced
2:00remaining
Identify the bug in Builder pattern usage
What is the bug in this test code using the Builder pattern?
JUnit
User user = new User.Builder()
    .name(null)
    .age(22)
    .build();

assertEquals("John", user.getName());
AThe test expects name "John" but Builder sets name to null
BBuilder does not initialize age, causing NullPointerException
CUser class constructor is missing, causing NullPointerException
DassertEquals cannot compare null values, causing NullPointerException
Attempts:
2 left
💡 Hint
Check what value the Builder sets for name and what the test expects.
🧠 Conceptual
advanced
1:30remaining
Why use Builder pattern for test data?
Which is the best reason to use the Builder pattern when creating test data objects in automated tests?
AIt automatically generates test reports after each test
BIt allows creating complex objects with readable code and optional parameters
CIt replaces the need for assertions in tests
DIt ensures tests run faster by caching objects
Attempts:
2 left
💡 Hint
Think about how Builder helps with object creation in tests.
framework
expert
2:30remaining
Integrating Builder pattern with JUnit 5 parameterized tests
How can you use the Builder pattern to supply different User objects to a JUnit 5 parameterized test?
JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.*;
import java.util.stream.Stream;

public class UserTest {

    static Stream<User> userProvider() {
        return Stream.of(
            new User.Builder().name("Anna").age(20).build(),
            new User.Builder().name("Ben").age(35).build()
        );
    }

    @ParameterizedTest
    @MethodSource("userProvider")
    void testUserAges(User user) {
        assertTrue(user.getAge() > 18);
    }
}
AMethodSource must return Strings, not User objects
BBuilder pattern cannot be used with parameterized tests
CThis code correctly uses Builder objects as parameters for the test
DParameterizedTest requires @ValueSource, not @MethodSource
Attempts:
2 left
💡 Hint
Check if the method source returns the correct type for the test parameter.