0
0
JUnittesting~10 mins

Choosing the right test double 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 mock object using Mockito.

JUnit
MyService service = Mockito.[1](MyService.class);
Drag options to blanks, or click blank then click option'
Aspy
Bmock
Cverify
Dwhen
Attempts:
3 left
💡 Hint
Common Mistakes
Using spy instead of mock when a full mock is needed.
Using verify to create objects, which is for checking calls.
2fill in blank
medium

Complete the code to verify that a method was called once on a mock.

JUnit
Mockito.[1](mockObject).someMethod();
Drag options to blanks, or click blank then click option'
Aspy
Bmock
Cverify
Dwhen
Attempts:
3 left
💡 Hint
Common Mistakes
Using mock() instead of verify() for checking calls.
Confusing spy with verify.
3fill in blank
hard

Fix the error in the code to stub a method call on a spy object.

JUnit
Mockito.[1](spyObject.someMethod()).thenReturn("value");
Drag options to blanks, or click blank then click option'
Amock
Bspy
Cverify
Dwhen
Attempts:
3 left
💡 Hint
Common Mistakes
Using verify instead of when for stubbing.
Using mock instead of when for stubbing.
4fill in blank
hard

Fill both blanks to create a spy and stub a method to return a value.

JUnit
MyService spyService = Mockito.[1](new MyService());
Mockito.[2](spyService.someMethod()).thenReturn("Hello");
Drag options to blanks, or click blank then click option'
Aspy
Bmock
Cwhen
Dverify
Attempts:
3 left
💡 Hint
Common Mistakes
Using mock instead of spy to wrap a real object.
Using verify instead of when for stubbing.
5fill in blank
hard

Fill all three blanks to mock a repository, stub a method, and verify the call.

JUnit
Repository repo = Mockito.[1](Repository.class);
Mockito.[2](repo.findById(1)).thenReturn(Optional.of(new Item()));
Mockito.[3](repo).findById(1);
Drag options to blanks, or click blank then click option'
Amock
Bwhen
Cverify
Dspy
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing spy and mock incorrectly.
Using verify before when.