Challenge - 5 Problems
Static Mocking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Mockito static mock test?
Consider the following JUnit test using Mockito 3.4+ to mock a static method. What will be the printed output when the test runs?
JUnit
import static org.mockito.Mockito.*; import org.mockito.MockedStatic; import org.junit.jupiter.api.Test; class Utility { static String greet() { return "Hello"; } } public class StaticMockTest { @Test void testStaticMock() { try (MockedStatic<Utility> mocked = mockStatic(Utility.class)) { mocked.when(Utility::greet).thenReturn("Hi"); System.out.println(Utility.greet()); } System.out.println(Utility.greet()); } }
Attempts:
2 left
💡 Hint
Think about the scope of the static mock and what happens after the try-with-resources block.
✗ Incorrect
Inside the try-with-resources block, the static method greet() is mocked to return "Hi". After the block, the mock is closed, so the original method returns "Hello" again.
❓ assertion
intermediate2:00remaining
Which assertion correctly verifies the static method was called exactly twice?
Given a mocked static method using Mockito 3.4+, which assertion correctly verifies that the static method Utility.greet() was called exactly two times?
JUnit
try (MockedStatic<Utility> mocked = mockStatic(Utility.class)) { mocked.when(Utility::greet).thenReturn("Hi"); Utility.greet(); Utility.greet(); // Which assertion below is correct? }
Attempts:
2 left
💡 Hint
Check how to verify calls on MockedStatic instances.
✗ Incorrect
The correct way is to call verify on the MockedStatic instance with a method reference and times(2). Option D is invalid because verify() is not used for static mocks that way. Option D passes a method call instead of a method reference. Option D is deprecated and incomplete.
🔧 Debug
advanced2:00remaining
Why does this Mockito static mock test fail with NullPointerException?
Examine the following test code. Why does it throw a NullPointerException during execution?
JUnit
import static org.mockito.Mockito.*; import org.mockito.MockedStatic; import org.junit.jupiter.api.Test; class Utility { static String greet() { return "Hello"; } } public class StaticMockTest { @Test void testStaticMock() { MockedStatic<Utility> mocked = mockStatic(Utility.class); mocked.when(Utility::greet).thenReturn(null); String result = Utility.greet().toUpperCase(); mocked.close(); } }
Attempts:
2 left
💡 Hint
Check what happens when you call a method on a null object.
✗ Incorrect
The mocked static method is set to return null. Then the test calls toUpperCase() on that null, causing NullPointerException.
❓ framework
advanced1:00remaining
Which Mockito version introduced static method mocking support?
Mockito added support for mocking static methods in which version?
Attempts:
1 left
💡 Hint
Check Mockito release notes for static mocking feature introduction.
✗ Incorrect
Mockito 3.4.0 was the first version to officially support static method mocking.
🧠 Conceptual
expert2:30remaining
What is a key limitation when mocking static methods with Mockito 3.4+?
Which of the following is a key limitation or consideration when mocking static methods using Mockito 3.4+?
Attempts:
2 left
💡 Hint
Think about resource management and test isolation.
✗ Incorrect
Static mocks affect the JVM globally, so they must be closed to avoid affecting other tests. This is why try-with-resources or manual close is required.