Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a simple stub for a dependency.
JUnit
MyService service = new MyService();
Dependency dep = new [1]();
service.setDependency(dep); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing MockDependency which is used for interaction verification.
✗ Incorrect
A stub provides fixed responses for testing without complex behavior.
2fill in blank
mediumComplete the code to verify that a method was called once using a mock.
JUnit
MockedList list = mock(MockedList.class); list.add("item"); verify(list, [1]).add("item");
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using times(2) which expects two calls.
✗ Incorrect
times(1) verifies the method was called exactly once.
3fill in blank
hardFix the error in the spy creation code.
JUnit
List<String> list = new ArrayList<>(); List<String> spyList = [1](list); spyList.add("test");
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using mock() which does not wrap the real object.
✗ Incorrect
spy() wraps a real object to track interactions while keeping behavior.
4fill in blank
hardFill both blanks to create a fake that simulates a database connection and returns a fixed value.
JUnit
class FakeDatabase implements Database { public String query(String sql) { return [1]; } } Database db = new FakeDatabase(); String result = db.query([2]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning dynamic result which is not fixed.
Using DROP TABLE which is destructive.
✗ Incorrect
A fake provides a simple implementation returning fixed data. The query string is a typical SQL command.
5fill in blank
hardFill all three blanks to create a mock, set expectation, and verify interaction.
JUnit
List<String> mockList = [1](List.class); when(mockList.get([2])).thenReturn("hello"); mockList.get(0); verify(mockList).get([3]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using spy() instead of mock().
Mismatching indexes in when() and verify().
✗ Incorrect
mock() creates the mock object. The index 0 is used for both when() and verify() calls.