0
0
JUnittesting~10 mins

doReturn and doThrow 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 method to return a value using doReturn.

JUnit
doReturn("mockedValue").when(mockedObject).[1]();
Drag options to blanks, or click blank then click option'
AsomeMethod
BthrowException
CverifyCall
Dexecute
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist in the mocked object.
Confusing doReturn with doThrow.
2fill in blank
medium

Complete the code to mock a method to throw an exception using doThrow.

JUnit
doThrow(new RuntimeException()).when(mockedObject).[1]();
Drag options to blanks, or click blank then click option'
AsomeMethod
Brun
CexecuteAction
DthrowException
Attempts:
3 left
💡 Hint
Common Mistakes
Using doReturn instead of doThrow for exceptions.
Mocking a method that does not throw exceptions.
3fill in blank
hard

Fix the error in the code to correctly mock a method to return a value using doReturn.

JUnit
doReturn("value").when([1]).someMethod();
Drag options to blanks, or click blank then click option'
AmockedObject
BdoReturn
CsomeMethod
DRuntimeException
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the method name inside when() instead of the mocked object.
Using exception classes instead of objects.
4fill in blank
hard

Fill both blanks to mock a method to throw an IllegalArgumentException.

JUnit
doThrow(new [1]()).when([2]).invalidMethod();
Drag options to blanks, or click blank then click option'
AIllegalArgumentException
BmockedService
CRuntimeException
DmockedObject
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong exception class.
Confusing the mocked object variable name.
5fill in blank
hard

Fill all three blanks to mock a method to return "success" and verify it was called once.

JUnit
doReturn("[1]").when([2]).process();
verify([3], times(1)).process();
Drag options to blanks, or click blank then click option'
Asuccess
BmockService
DmockedObject
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for when() and verify().
Forgetting to match the return value string.