0
0
JUnittesting~20 mins

Mocking static methods (Mockito 3.4+) in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Static Mocking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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());
    }
}
AHello\nHello
BHi\nHello
CHi\nHi
DHello\nHi
Attempts:
2 left
💡 Hint
Think about the scope of the static mock and what happens after the try-with-resources block.
assertion
intermediate
2: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?
}
Averify(Utility.class, times(2)).greet();
BverifyStatic(Utility.class, times(2)); Utility.greet();
Cmocked.verify(Utility.greet(), times(2));
Dmocked.verify(Utility::greet, times(2));
Attempts:
2 left
💡 Hint
Check how to verify calls on MockedStatic instances.
🔧 Debug
advanced
2: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();
    }
}
AUtility.greet() returns null, so calling toUpperCase() causes NullPointerException.
BmockStatic() was not called inside try-with-resources, causing resource leak and failure.
CMockito cannot mock static methods returning String, causing NullPointerException.
DThe mocked.when() syntax is incorrect, causing NullPointerException.
Attempts:
2 left
💡 Hint
Check what happens when you call a method on a null object.
framework
advanced
1:00remaining
Which Mockito version introduced static method mocking support?
Mockito added support for mocking static methods in which version?
A4.0.0
B2.28.0
C3.4.0
D3.0.0
Attempts:
1 left
💡 Hint
Check Mockito release notes for static mocking feature introduction.
🧠 Conceptual
expert
2: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+?
AStatic mocks must be used within try-with-resources or closed manually to avoid side effects.
BStatic methods can only be mocked if they are public and non-final.
CMockito requires the static method to be in the same package as the test class.
DStatic method mocking disables all other mocks in the test class.
Attempts:
2 left
💡 Hint
Think about resource management and test isolation.