0
0
JUnittesting~10 mins

Object Mother pattern in JUnit - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a default User object using the Object Mother pattern.

JUnit
User user = UserMother.[1]();
Drag options to blanks, or click blank then click option'
AgetDefaultUser
BcreateDefaultUser
CmakeUser
DbuildUser
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names that do not follow Object Mother naming conventions.
Confusing factory methods with Object Mother methods.
2fill in blank
medium

Complete the code to assert that the User object created by the Object Mother has the expected default name.

JUnit
assertEquals("John Doe", user.[1]());
Drag options to blanks, or click blank then click option'
AgetId
BgetEmail
CgetName
DgetUsername
Attempts:
3 left
💡 Hint
Common Mistakes
Using getId() or getEmail() which do not return the name.
Using incorrect method names that cause compilation errors.
3fill in blank
hard

Fix the error in the Object Mother method that creates a User with a custom email.

JUnit
public static User createUserWithEmail(String email) {
    return new User("John Doe", [1]);
}
Drag options to blanks, or click blank then click option'
A"email"
Bemail
CgetEmail()
DuserEmail
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the string "email" instead of the variable.
Using undefined variables like userEmail.
Calling methods that do not exist in this context.
4fill in blank
hard

Fill both blanks to create a User with a custom name and default email using the Object Mother pattern.

JUnit
public static User [1](String name) {
    return new User(name, [2]);
}
Drag options to blanks, or click blank then click option'
AcreateUserWithName
B"default@example.com"
CgetDefaultEmail()
DcreateDefaultUser
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names that do not match the purpose.
Passing method calls instead of string literals for the email.
5fill in blank
hard

Fill all three blanks to create a map of User names to their emails for users with names longer than 5 characters.

JUnit
Map<String, String> userMap = users.stream()
    .filter(user -> user.getName().length() [1] 5)
    .collect(Collectors.toMap(
        user -> user.[2](),
        user -> user.[3]()
    ));
Drag options to blanks, or click blank then click option'
A>
BgetName
CgetEmail
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > in the filter.
Swapping getName() and getEmail() in the map.
Using incorrect method names causing compilation errors.