Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start mocking static methods using Mockito.
JUnit
try (MockedStatic<Utility> utilities = Mockito.[1](Utility.class)) { // test code }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'whenStatic' instead of 'mockStatic'.
Trying to mock static methods without using try-with-resources.
✗ Incorrect
Use Mockito.mockStatic(Class.class) to create a mock for static methods of a class.
2fill in blank
mediumComplete the code to stub a static method to return a specific value.
JUnit
utilities.[1](() -> Utility.staticMethod()).thenReturn("mocked value");
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'doReturn' which is for instance methods.
Trying to stub without a lambda expression.
✗ Incorrect
Use 'when' to specify the static method call to stub and then define the return value.
3fill in blank
hardFix the error in the code to verify a static method was called once.
JUnit
utilities.verify(() -> Utility.staticMethod(), [1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like 'calledOnce' or 'once'.
Omitting the verification mode argument.
✗ Incorrect
Use 'times(1)' to verify the static method was called exactly once.
4fill in blank
hardFill both blanks to mock two different static methods with different return values.
JUnit
utilities.[1](() -> Utility.firstStatic()).thenReturn(10); utilities.[2](() -> Utility.secondStatic()).thenReturn(20);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different stubbing methods inconsistently.
Trying to stub without lambda expressions.
✗ Incorrect
Use 'when' to stub each static method call with the desired return value.
5fill in blank
hardFill all three blanks to correctly open, stub, and close the static mock.
JUnit
try (MockedStatic<Utility> utilities = Mockito.[1](Utility.class)) { utilities.[2](() -> Utility.staticMethod()).thenReturn("done"); } // [3] closes the mock automatically
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use try-with-resources to close the mock.
Using incorrect methods to open or stub the mock.
✗ Incorrect
Use 'mockStatic' to open the mock, 'when' to stub the method, and 'try-with-resources' ensures automatic closing.