0
0
JUnittesting~10 mins

when().thenReturn() stubbing 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 stub the method call using Mockito.

JUnit
when(mockedList.[1]()).thenReturn("hello");
Drag options to blanks, or click blank then click option'
Aget(0)
Badd("test")
Cclear()
Dsize()
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to stub void methods like clear() or add() which do not return values.
Using size() which returns an int, not a String.
2fill in blank
medium

Complete the code to stub the size() method to return 5.

JUnit
when(mockedList.[1]()).thenReturn(5);
Drag options to blanks, or click blank then click option'
Asize()
Blength()
Csize
Dget(0)
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting parentheses in method call.
Using incorrect method names like size or length().
3fill in blank
hard

Fix the error in the stubbing code below.

JUnit
when(mockedList.get([1])).thenReturn("world");
Drag options to blanks, or click blank then click option'
Anull
B"0"
C0
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string instead of an integer index.
Using null or negative index values.
4fill in blank
hard

Fill both blanks to stub mockedMap.get() to return 10 when key "score" is used.

JUnit
when(mockedMap.[1]([2])).thenReturn(10);
Drag options to blanks, or click blank then click option'
Aget
B"score"
Cput
Dremove
Attempts:
3 left
💡 Hint
Common Mistakes
Using put or remove which are void or different methods.
Not passing the key as a string.
5fill in blank
hard

Fill all three blanks to stub mockedService.calculate() to return 42 when called with 5.

JUnit
when(mockedService.[1]([2])).thenReturn([3]);
Drag options to blanks, or click blank then click option'
Acalculate
B5
C42
Dcompute
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method names like compute.
Passing arguments or return values as strings instead of numbers.