What if you could test your code without waiting for real parts or risking failures from outside problems?
Why Mock objects in JUnit? - Purpose & Use Cases
Imagine testing a car's navigation system that needs live GPS data. Without mocks, you must wait for real GPS signals every time you test, which is slow and unreliable.
Manually testing with real components is slow, unpredictable, and can cause tests to fail due to external issues like network or hardware problems, making it hard to trust test results.
Mock objects act like pretend parts that behave exactly as needed for tests. They replace real components, letting you control responses and test scenarios quickly and reliably.
RealGPS gps = new RealGPS(); Navigation nav = new Navigation(gps); nav.calculateRoute();
GPS mockGPS = mock(GPS.class); when(mockGPS.getLocation()).thenReturn(new Location(10, 20)); Navigation nav = new Navigation(mockGPS); nav.calculateRoute();
Mock objects let you test parts of your code in isolation, making tests fast, stable, and easy to write.
Testing a payment system without actually charging credit cards by mocking the payment gateway responses.
Manual testing with real components is slow and unreliable.
Mock objects simulate parts to control test behavior.
This leads to faster, more stable, and focused tests.
Practice
mock objects in JUnit testing?Solution
Step 1: Understand the role of mock objects
Mock objects imitate real objects to isolate the code being tested and control responses.Step 2: Identify the main benefit in testing
This helps test code without relying on actual implementations or external systems.Final Answer:
To simulate real objects and control their behavior during tests -> Option BQuick Check:
Mock objects = simulate and control behavior [OK]
- Thinking mocks speed up compilation
- Confusing mocks with test data generators
- Believing mocks replace the JUnit framework
Solution
Step 1: Recall Mockito syntax for creating mocks
Mockito usesmock(Class.class)to create mock objects.Step 2: Match the syntax to options
MyClass mock = mock(MyClass.class); matches the correct syntax:MyClass mock = mock(MyClass.class);Final Answer:
MyClass mock = mock(MyClass.class); -> Option AQuick Check:
Mockito mock creation = mock(Class.class) [OK]
- Using new Mock<>() which is not valid
- Calling mock() on an instance
- Using createMock which is not Mockito syntax
result be after execution?List<String> mockList = mock(List.class); when(mockList.size()).thenReturn(5); int result = mockList.size();
Solution
Step 1: Understand the mock setup
The mock list'ssize()method is stubbed to return 5 when called.Step 2: Evaluate the method call
CallingmockList.size()returns 5 as per the stub, not the real list size.Final Answer:
5 -> Option AQuick Check:
Stubbed method returns set value 5 [OK]
- Expecting default 0 for int return
- Thinking mock calls throw exceptions by default
- Confusing mock with real object behavior
MyService service = mock(MyService.class);
when(service.getData()).thenReturn("data");
verify(service).getData();Solution
Step 1: Understand verify usage in Mockito
Verification checks if a method was called on the mock during the test.Step 2: Check if method was called before verify
The code never callsservice.getData()beforeverify, so verification fails.Final Answer:
Missing call toservice.getData()before verify -> Option DQuick Check:
Verify requires method call before checking [OK]
- Verifying without calling the method
- Confusing mock creation syntax
- Thinking verify only works on void methods
getValue()?Solution
Step 1: Recall Mockito chaining for multiple returns
Mockito allows chainingthenReturn()calls to specify sequential return values.Step 2: Match chaining syntax to options
when(mock.getValue()).thenReturn("first").thenReturn("second");matches the correct chaining syntax for sequential calls.Final Answer:
when(mock.getValue()).thenReturn("first").thenReturn("second"); -> Option CQuick Check:
Chain thenReturn() for multiple call returns [OK]
- Passing multiple values in one thenReturn() call
- Overwriting stubs instead of chaining
- Reversing the order of return values
Ready to prove it?
Pass the test and this pattern moves unknown
