Challenge - 5 Problems
Mock Objects Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple Mockito mock behavior
What is the output of the following JUnit test using Mockito when the mocked method is called without stubbing?
JUnit
import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class Service { String getData() { return "Real Data"; } } public class MockTest { @Test void testMock() { Service mockService = mock(Service.class); String result = mockService.getData(); System.out.println(result); assertNull(result); } }
Attempts:
2 left
💡 Hint
Think about what Mockito returns by default for unstubbed methods that return objects.
✗ Incorrect
By default, Mockito returns null for methods returning objects if no behavior is stubbed. So calling getData() on a mock returns null, not the real method's value.
❓ assertion
intermediate1:30remaining
Correct Mockito verification syntax
Which of the following Mockito verification statements correctly verifies that the method process() was called exactly twice on a mock object named mockProcessor?
Attempts:
2 left
💡 Hint
Check the Mockito syntax for verifying exact number of invocations.
✗ Incorrect
The correct syntax to verify exact number of calls is verify(mock, times(n)).method(). Option D uses times(2) correctly.
🔧 Debug
advanced2:30remaining
Identify the cause of NullPointerException in Mockito test
Given the following test code, why does it throw a NullPointerException?
JUnit
import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class Repository { String fetch() { return "data"; } } class Service { private Repository repo; Service(Repository repo) { this.repo = repo; } String getData() { return repo.fetch().toUpperCase(); } } public class NullPointerTest { @Test void testService() { Repository mockRepo = mock(Repository.class); Service service = new Service(mockRepo); String result = service.getData(); assertEquals("DATA", result); } }
Attempts:
2 left
💡 Hint
What does Mockito return for unstubbed methods that return objects?
✗ Incorrect
Mockito returns null for unstubbed methods. So mockRepo.fetch() returns null, and calling toUpperCase() on null causes NullPointerException.
🧠 Conceptual
advanced1:30remaining
Purpose of using mock objects in unit testing
What is the main reason to use mock objects in unit testing?
Attempts:
2 left
💡 Hint
Think about why we want to control external parts when testing a single unit.
✗ Incorrect
Mocks help isolate the unit by simulating dependencies, allowing focused and reliable tests without relying on real external components.
❓ framework
expert3:00remaining
Behavior of Mockito spy vs mock
Consider the following code snippet using Mockito. What will be the output when calling spyList.get(0)?
JUnit
import static org.mockito.Mockito.*; import java.util.*; import org.junit.jupiter.api.Test; public class SpyTest { @Test void testSpy() { List<String> list = new ArrayList<>(); list.add("one"); List<String> spyList = spy(list); when(spyList.get(0)).thenReturn("two"); System.out.println(spyList.get(0)); } }
Attempts:
2 left
💡 Hint
Remember that spy calls real methods unless stubbed.
✗ Incorrect
A spy calls the real method unless stubbed. Here get(0) is stubbed to return "two", so it prints "two".