0
0
JUnittesting~10 mins

Fake objects 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 fake object for testing.

JUnit
FakeDatabase fakeDb = mock(FakeDatabase.class);
fakeDb.[1]("testUser");
Drag options to blanks, or click blank then click option'
AaddUser
BupdateUser
Cconnect
DdeleteUser
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.
2fill in blank
medium

Complete 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'
AupdateUser
Bconnect
CaddUser
DdeleteUser
Attempts:
3 left
💡 Hint
Common Mistakes
Verifying connect does not check user addition.
Verifying deleteUser or updateUser is unrelated to adding a user.
3fill in blank
hard

Fix 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'
Aconnect
BuserExists
CaddUser
DdeleteUser
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to stub addUser which might be void causes errors.
connect and deleteUser do not return boolean values.
4fill in blank
hard

Fill 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'
AFakeDatabase
BRealDatabase
CuserExists
DaddUser
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.
5fill in blank
hard

Fill 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'
AFakeDatabase
BRealDatabase
CuserExists
DaddUser
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.