Recall & Review
beginner
What is mocking static methods in Mockito 3.4+?
Mocking static methods means creating fake versions of static methods to control their behavior during tests, allowing you to test code that depends on static methods without running the real ones.
Click to reveal answer
beginner
How do you start mocking static methods in Mockito 3.4+?
You use the try-with-resources block with Mockito.mockStatic(ClassName.class) to create a mock scope for static methods, ensuring the mock is closed after use.
Click to reveal answer
intermediate
Why should you use try-with-resources when mocking static methods?
Because it automatically closes the static mock after the test, preventing side effects on other tests and keeping tests isolated.
Click to reveal answer
beginner
What is the syntax to mock a static method call to return a specific value?
Inside the mockStatic block, use mockStatic.when(() -> ClassName.staticMethod(args)).thenReturn(value) to specify the fake return value.
Click to reveal answer
intermediate
Can you verify static method calls with Mockito 3.4+? How?
Yes, inside the mockStatic block, use mockStatic.verify(() -> ClassName.staticMethod(args)) to check if the static method was called as expected.
Click to reveal answer
Which Mockito feature allows mocking static methods starting from version 3.4+?
✗ Incorrect
Mockito.mockStatic() is the method introduced in Mockito 3.4+ to mock static methods.
Why is try-with-resources recommended when mocking static methods?
✗ Incorrect
Try-with-resources ensures the static mock is closed after use, preventing interference with other tests.
How do you specify a return value for a mocked static method?
✗ Incorrect
Inside the mockStatic block, use when(...).thenReturn(...) to define the return value.
Can you verify static method calls in Mockito 3.4+?
✗ Incorrect
Mockito 3.4+ allows verification of static method calls inside the mockStatic block.
What happens if you don't close the static mock after testing?
✗ Incorrect
Not closing the static mock can cause side effects on other tests, breaking test isolation.
Explain how to mock a static method using Mockito 3.4+ and why it is important to use try-with-resources.
Think about resource management and test isolation.
You got /4 concepts.
Describe how to verify that a static method was called during a test using Mockito 3.4+.
Verification is similar to instance method verification but inside the static mock scope.
You got /3 concepts.