0
0
JUnittesting~20 mins

Object Mother pattern in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Object Mother Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Purpose of the Object Mother pattern
What is the main purpose of using the Object Mother pattern in unit testing with JUnit?
ATo mock external dependencies automatically without writing any code
BTo centralize and reuse complex test object creation to reduce duplication and improve readability
CTo generate random test data for performance testing
DTo replace the need for assertions in test methods
Attempts:
2 left
💡 Hint
Think about how test objects are created and reused in multiple tests.
Predict Output
intermediate
2: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());
    }
}
ATest passes because the adult user is correctly identified
BTest fails because the age is incorrectly set
CTest fails due to a NullPointerException
DTest fails because the isAdult method is not implemented
Attempts:
2 left
💡 Hint
Check the age value and the isAdult method logic.
assertion
advanced
1: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();
AassertTrue(childUser.isAdult());
BassertEquals(true, childUser.isAdult());
CassertNull(childUser);
DassertFalse(childUser.isAdult());
Attempts:
2 left
💡 Hint
A child user should not be an adult.
🔧 Debug
advanced
2: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?
AThe assertTrue method is used incorrectly
BThe isAdult method always returns false
CThe createAdultUser method returns a User with age less than 18
DThe User class constructor is missing
Attempts:
2 left
💡 Hint
Check the age value set in the Object Mother method.
framework
expert
2: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?
ACreate a separate class with static factory methods for test objects and call them in test methods
BInstantiate test objects directly inside each test method without helpers
CUse @BeforeEach to create all test objects and store them in instance variables
DUse mocking frameworks to replace Object Mother completely
Attempts:
2 left
💡 Hint
Think about reusability and clarity of test object creation.