Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a fake object for testing.
JUnit
FakeDatabase fakeDb = mock(FakeDatabase.class); fakeDb.[1]("testUser");
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using connect instead of addUser will not add a user.
Using deleteUser or updateUser will not add a new user.
✗ Incorrect
The method addUser is used to add a user to the fake database for testing purposes.
2fill in blank
mediumComplete the code to verify the fake object's method was called.
JUnit
verify(fakeDb).[1]("testUser");
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Verifying connect does not check user addition.
Verifying deleteUser or updateUser is unrelated to adding a user.
✗ Incorrect
We verify that addUser was called on the fake database with the argument "testUser".
3fill in blank
hardFix the error in the fake object setup to return a value.
JUnit
when(fakeDb.[1]("testUser")).thenReturn(true);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to stub addUser which might be void causes errors.
connect and deleteUser do not return boolean values.
✗ Incorrect
The method userExists returns a boolean, so it can be stubbed to return true.
4fill in blank
hardFill both blanks to create a fake object and stub a method.
JUnit
FakeDatabase fakeDb = mock([1].class); when(fakeDb.[2]("admin")).thenReturn(true);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using RealDatabase instead of FakeDatabase defeats the purpose of a fake.
Stubbing addUser instead of userExists will not return a boolean.
✗ Incorrect
We create a FakeDatabase object and stub the userExists method to return true for "admin".
5fill in blank
hardFill all three blanks to verify method calls and stub behavior on a fake object.
JUnit
FakeDatabase fakeDb = mock([1].class); when(fakeDb.[2]("guest")).thenReturn(false); verify(fakeDb).[3]("guest");
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using RealDatabase instead of FakeDatabase.
Verifying addUser instead of userExists causes test failures.
Stubbing addUser which does not return a value.
✗ Incorrect
We create a FakeDatabase, stub userExists to return false for "guest", and verify userExists was called with "guest".