0
0
JUnittesting~10 mins

Mocking void methods 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 mock a void method using Mockito.

JUnit
doNothing().when(mockedObject).[1]();
Drag options to blanks, or click blank then click option'
AvoidMethod()
BvoidMethod(void)
CvoidMethod
DvoidMethod{}
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting parentheses after the method name inside when(), causing syntax errors.
2fill in blank
medium

Complete the code to verify that a void method was called exactly once.

JUnit
verify(mockedObject, [1]()).voidMethod();
Drag options to blanks, or click blank then click option'
Atimes(0)
BatLeastOnce()
Cnever()
Dtimes(1)
Attempts:
3 left
💡 Hint
Common Mistakes
Using times(0) or never() when expecting the method to be called once.
3fill in blank
hard

Fix the error in the code to stub a void method to throw an exception.

JUnit
doThrow(new RuntimeException()).when(mockedObject).[1]();
Drag options to blanks, or click blank then click option'
AvoidMethod{}
BvoidMethod[]
CvoidMethod()
DvoidMethod
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting parentheses after the method name inside when(), causing syntax errors.
4fill in blank
hard

Fill both blanks to stub a void method to do nothing and verify it was called twice.

JUnit
do[1]().when(mockedObject).voidMethod();
verify(mockedObject, [2]()).voidMethod();
Drag options to blanks, or click blank then click option'
ANothing
Btimes(1)
Ctimes(2)
DThrow(new RuntimeException())
Attempts:
3 left
💡 Hint
Common Mistakes
Using times(1) when expecting two calls.
Using doThrow() instead of doNothing() when no exception is expected.
5fill in blank
hard

Fill all three blanks to stub a void method to throw an exception, verify it was called once, and reset the mock.

JUnit
do[1](new RuntimeException()).when(mockedObject).voidMethod();
verify(mockedObject, [2]()).voidMethod();
[3](mockedObject);
Drag options to blanks, or click blank then click option'
ANothing
Btimes(1)
Creset
DThrow
Attempts:
3 left
💡 Hint
Common Mistakes
Using doNothing() instead of doThrow() when expecting an exception.
Forgetting to reset the mock after verification.