Challenge - 5 Problems
Builder Pattern Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()); } }
Attempts:
2 left
💡 Hint
Check how the Builder sets the fields before building the User object.
✗ Incorrect
The Builder pattern sets name to "Alice" and age to 25 before building the User. The test asserts these values correctly, so it passes.
❓ assertion
intermediate1: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();
Attempts:
2 left
💡 Hint
Use the assertion that compares expected and actual values with correct types.
✗ Incorrect
assertEquals(30, user.getAge()) compares the expected int 30 with the actual int age correctly. Option A compares int to String, causing failure.
🔧 Debug
advanced2: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());Attempts:
2 left
💡 Hint
Check what value the Builder sets for name and what the test expects.
✗ Incorrect
The Builder sets name to null explicitly, but the test expects "John". This mismatch causes the assertion to fail.
🧠 Conceptual
advanced1: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?
Attempts:
2 left
💡 Hint
Think about how Builder helps with object creation in tests.
✗ Incorrect
Builder pattern helps create complex test data with clear, readable code and optional fields, improving test maintainability.
❓ framework
expert2: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); } }
Attempts:
2 left
💡 Hint
Check if the method source returns the correct type for the test parameter.
✗ Incorrect
JUnit 5 supports parameterized tests with @MethodSource returning any object type. Using Builder to create User objects for test parameters is valid.