Challenge - 5 Problems
Object Mother Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
Purpose of the Object Mother pattern
What is the main purpose of using the Object Mother pattern in unit testing with JUnit?
Attempts:
2 left
💡 Hint
Think about how test objects are created and reused in multiple tests.
✗ Incorrect
The Object Mother pattern helps create and reuse complex test objects in one place, making tests cleaner and easier to maintain.
❓ Predict Output
intermediate2:00remaining
Output of JUnit test using Object Mother
Given the following JUnit test code using an Object Mother, what will be the test result?
JUnit
public class User { private String name; private int age; public User(String name, int age) { this.name = name; this.age = age; } public boolean isAdult() { return age >= 18; } } public class UserObjectMother { public static User createAdultUser() { return new User("Alice", 30); } public static User createChildUser() { return new User("Bob", 10); } } import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; public class UserTest { @Test public void testAdultUser() { User user = UserObjectMother.createAdultUser(); assertTrue(user.isAdult()); } }
Attempts:
2 left
💡 Hint
Check the age value and the isAdult method logic.
✗ Incorrect
The Object Mother creates a user with age 30, which is adult. The assertion expects true, so the test passes.
❓ assertion
advanced1:30remaining
Correct assertion for Object Mother created object
Which assertion correctly verifies that a User created by the Object Mother is a child (under 18)?
JUnit
User childUser = UserObjectMother.createChildUser();
Attempts:
2 left
💡 Hint
A child user should not be an adult.
✗ Incorrect
The isAdult method returns false for a child user, so assertFalse correctly verifies this.
🔧 Debug
advanced2:00remaining
Debugging Object Mother test failure
A test using the Object Mother pattern fails with this error: java.lang.AssertionError: expected: but was:. The test code is:
User user = UserObjectMother.createAdultUser();
assertTrue(user.isAdult());
What is the most likely cause?
Attempts:
2 left
💡 Hint
Check the age value set in the Object Mother method.
✗ Incorrect
If createAdultUser returns a user with age under 18, isAdult returns false, causing the assertion to fail.
❓ framework
expert2:30remaining
Best practice for Object Mother usage in JUnit 5
In JUnit 5, which approach best integrates the Object Mother pattern to improve test maintainability and readability?
Attempts:
2 left
💡 Hint
Think about reusability and clarity of test object creation.
✗ Incorrect
Using a separate class with static methods (Object Mother) centralizes object creation, making tests cleaner and easier to maintain.