0
0
JUnittesting~10 mins

Why advanced mocking handles complex dependencies in JUnit - Test Your Understanding

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'
Amock
Bspy
Cverify
Dwhen
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'spy' instead of 'mock' creates a partial mock, not a full mock.
Using 'verify' or 'when' does not create mocks.
2fill in blank
medium

Complete the code to stub a method call to return a specific value.

JUnit
Mockito.[1](service.getData()).thenReturn("mocked data");
Drag options to blanks, or click blank then click option'
Aspy
Bverify
Cmock
Dwhen
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'verify' instead of 'when' is for checking calls, not stubbing.
Using 'mock' or 'spy' here is incorrect.
3fill in blank
hard

Fix the error in verifying that a method was called exactly once.

JUnit
Mockito.verify(service, Mockito.[1]()).process();
Drag options to blanks, or click blank then click option'
Atimes(2)
Btimes(1)
Conce()
Dnever()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'once()' causes a compilation error because it is not a valid Mockito method.
Using 'times(2)' checks for two calls, not one.
4fill in blank
hard

Fill both blanks to mock a complex dependency and stub its method.

JUnit
Dependency dep = Mockito.[1](Dependency.class);
Mockito.[2](dep.getValue()).thenReturn(42);
Drag options to blanks, or click blank then click option'
Amock
Bwhen
Cspy
Dverify
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'spy' instead of 'mock' creates a partial mock, which may not be desired.
Using 'verify' instead of 'when' is incorrect for stubbing.
5fill in blank
hard

Fill all three blanks to verify a method call with argument matcher and times.

JUnit
Mockito.verify(service, Mockito.[1](3)).execute(Mockito.[2]("test"), Mockito.[3]());
Drag options to blanks, or click blank then click option'
Atimes
Beq
Cany
Dnever
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'never' instead of 'times' causes verification to expect zero calls.
Using 'any' instead of 'eq' loses exact argument matching.