It is a design pattern that creates reusable methods to build test objects with default data, making tests easier to write and read.
Click to reveal answer
beginner
Why use the Object Mother pattern instead of creating objects directly in tests?
Because it reduces duplicate code, centralizes test object creation, and makes tests cleaner and easier to maintain.
Click to reveal answer
intermediate
Show a simple example of an Object Mother method in Java using JUnit.
public class UserObjectMother {
public static User createDefaultUser() {
return new User("Alice", "alice@example.com", 30);
}
}
Click to reveal answer
intermediate
How does the Object Mother pattern improve test readability?
By giving meaningful names to object creation methods, tests read like sentences and focus on behavior, not setup details.
Click to reveal answer
advanced
What is a potential downside of the Object Mother pattern?
If not managed well, it can grow large and complex, making it hard to understand which data is used in tests.
Click to reveal answer
What is the main purpose of the Object Mother pattern?
ATo replace unit tests with integration tests
BTo mock external services in tests
CTo generate random test data at runtime
DTo create reusable test objects with default data
✗ Incorrect
The Object Mother pattern focuses on creating reusable test objects with default data to simplify test setup.
Which of the following is a benefit of using Object Mother pattern?
ACentralizes test object creation
BMakes tests harder to read
CIncreases duplicate code in tests
DRemoves the need for assertions
✗ Incorrect
Centralizing test object creation reduces duplication and improves maintainability.
In JUnit, where would you typically place Object Mother classes?
AIn the production database
BInside the main application source folder
CIn the test source folder alongside tests
DIn the build configuration files
✗ Incorrect
Object Mother classes belong in the test source folder because they support test code only.
What should Object Mother methods return?
ANull values to simulate errors
BTest objects with default or common data
CStrings describing test cases
DTest reports
✗ Incorrect
They return test objects pre-filled with default or typical data for reuse.
Which is NOT a good practice when using Object Mother pattern?
ACreating one huge Object Mother with all test data
BGiving methods descriptive names
CKeeping Object Mother classes small and focused
DUsing Object Mother to reduce test setup code
✗ Incorrect
Creating one huge Object Mother class can make maintenance difficult and reduce clarity.
Explain the Object Mother pattern and how it helps in writing unit tests.
Think about how you prepare objects for many tests without repeating code.
You got /4 concepts.
Describe a simple example of an Object Mother method in Java and how you would use it in a JUnit test.
Imagine a method that creates a user with default info for tests.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the Object Mother pattern in JUnit testing?
easy
A. To generate random test data dynamically
B. To create reusable test objects to reduce setup code
C. To mock external services during tests
D. To organize test cases into groups
Solution
Step 1: Understand Object Mother role
The Object Mother pattern provides ready-made objects for tests to avoid repeating setup code.
Step 2: Compare with other options
Mocking services or grouping tests are different patterns; Object Mother focuses on reusable test objects.
Final Answer:
To create reusable test objects to reduce setup code -> Option B
Quick Check:
Object Mother = reusable test objects [OK]
Hint: Object Mother = reusable test objects for simpler tests [OK]
Common Mistakes:
Confusing Object Mother with mocking frameworks
Thinking it generates random data automatically
Believing it organizes test suites
2. Which of the following is the correct way to define an Object Mother method in JUnit for a User object?
easy
A. public User createUser() { return new User("Alice", 30); }
B. User createUser() { return new User("Alice", 30); }
C. public static User createUser() { return new User("Alice", 30); }
D. static User createUser() { return new User("Alice", 30); }
Solution
Step 1: Check method signature for Object Mother
Object Mother methods are usually public static so tests can call them without creating an instance.
Step 2: Validate return type and body
The method returns a new User object with fixed data, matching the pattern.
Final Answer:
public static User createUser() { return new User("Alice", 30); } -> Option C
Quick Check:
Object Mother methods are public static [OK]
Hint: Object Mother methods are public static for easy reuse [OK]
Common Mistakes:
Omitting static keyword
Using non-public access modifier
Missing return type
3. Given this Object Mother method:
public static Order createOrder() {
return new Order("123", 5, 100.0);
}
What will be the output of this test?
@Test
void testOrderTotal() {
Order order = ObjectMother.createOrder();
assertEquals(500.0, order.getTotal());
}
medium
A. Test passes because total is 500.0
B. Test fails because total is 100.0
C. Test fails due to NullPointerException
D. Test fails because order ID is incorrect
Solution
Step 1: Calculate expected total from Object Mother data
Order has quantity 5 and price 100.0, so total = 5 * 100.0 = 500.0.
Step 2: Check assertion in test
The test asserts total equals 500.0, which matches the calculated total.
Final Answer:
Test passes because total is 500.0 -> Option A
Quick Check:
Quantity * Price = Total = 500.0 [OK]
Hint: Multiply quantity and price to find total [OK]
Common Mistakes:
Assuming total is price only
Ignoring quantity in calculation
Expecting NullPointerException without cause
4. Identify the error in this Object Mother method:
public static Product createProduct() {
Product p = new Product();
p.setName("Laptop");
return p;
p.setPrice(1200);
}
medium
A. Code after return statement is unreachable
B. Missing static keyword in method
C. Product constructor requires parameters
D. Method should return void
Solution
Step 1: Analyze method flow
The return p; line ends the method, so code after it is unreachable.
Step 2: Identify unreachable code
The line p.setPrice(1200); after return will never run, causing a compile warning or error.
Final Answer:
Code after return statement is unreachable -> Option A
Quick Check:
Code after return is unreachable [OK]
Hint: No code runs after return statement [OK]
Common Mistakes:
Ignoring unreachable code warnings
Assuming method can run code after return
Confusing method return type
5. You have multiple tests needing different User objects: one active, one inactive, and one admin. How should you design your Object Mother class to best support this?
hard
A. Use a mocking framework instead of Object Mother
B. Create one method createUser(String type) with if-else inside
C. Create User objects directly in each test without Object Mother
D. Create separate static methods: activeUser(), inactiveUser(), adminUser()
Solution
Step 1: Understand Object Mother best practice
Separate static methods for each test object type keep tests clear and Object Mother simple.
Step 2: Compare alternatives
Using one method with if-else adds complexity; creating objects in tests repeats code; mocking is different purpose.
Final Answer:
Create separate static methods: activeUser(), inactiveUser(), adminUser() -> Option D
Quick Check:
Separate methods = clear reusable objects [OK]
Hint: Use separate methods for each test object type [OK]
Common Mistakes:
Overloading one method with many conditions
Not using Object Mother and repeating code
Confusing mocking with object creation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.