0
0
JUnittesting~10 mins

Argument captors 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 an argument captor for a String argument.

JUnit
ArgumentCaptor<String> captor = ArgumentCaptor.[1];
Drag options to blanks, or click blank then click option'
AforClass(String.class)
Bcapture()
CnewInstance()
Dof(String.class)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'capture()' instead of 'forClass()' to create the captor.
Trying to instantiate ArgumentCaptor directly with 'new'.
2fill in blank
medium

Complete the code to verify a method call and capture its argument.

JUnit
verify(mockObject).someMethod([1]);
Drag options to blanks, or click blank then click option'
Acaptor.capture()
Beq("test")
CanyString()
DisNull()
Attempts:
3 left
💡 Hint
Common Mistakes
Using matchers like 'anyString()' which do not capture the argument.
Passing a fixed value like 'eq("test")' instead of capturing.
3fill in blank
hard

Fix the error in retrieving the captured value from the captor.

JUnit
String capturedValue = captor.[1]();
Drag options to blanks, or click blank then click option'
Aget
BgetAllValues
CgetValue
Dcapture
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'getAllValues' which returns a list, not a single value.
Using 'capture' which is for capturing, not retrieving.
4fill in blank
hard

Fill both blanks to capture an argument and assert its value.

JUnit
verify(mock).process([1]);
assertEquals("expected", captor.[2]());
Drag options to blanks, or click blank then click option'
Acapture()
BgetValue()
Ccapture
DgetValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'capture' without parentheses in the first blank.
Using 'getValue' without parentheses in the second blank.
5fill in blank
hard

Fill all three blanks to capture multiple arguments and assert their values.

JUnit
verify(mock, times(2)).update([1]);
List<String> values = captor.[2]();
assertEquals("first", values.get([3]));
Drag options to blanks, or click blank then click option'
Acapture()
BgetAllValues
C0
DgetValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'getValue' instead of 'getAllValues' to retrieve multiple arguments.
Using '1' instead of '0' as the index for the first element.