0
0
JUnittesting~20 mins

Mock objects in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mock Objects Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
    }
}
Anull
B"Real Data"
CThrows NullPointerException
DEmpty string ""
Attempts:
2 left
💡 Hint
Think about what Mockito returns by default for unstubbed methods that return objects.
assertion
intermediate
1: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?
Averify(mockProcessor, times(1)).process();
Bverify(mockProcessor).process(2);
Cverify(mockProcessor, atLeast(2)).process();
Dverify(mockProcessor, times(2)).process();
Attempts:
2 left
💡 Hint
Check the Mockito syntax for verifying exact number of invocations.
🔧 Debug
advanced
2: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);
    }
}
AThe Service constructor is not called properly, so repo is null.
BMockito mock creation failed due to missing annotation.
CThe mockRepo.fetch() returns null by default, so calling toUpperCase() causes NullPointerException.
DassertEquals is used incorrectly causing the exception.
Attempts:
2 left
💡 Hint
What does Mockito return for unstubbed methods that return objects?
🧠 Conceptual
advanced
1:30remaining
Purpose of using mock objects in unit testing
What is the main reason to use mock objects in unit testing?
ATo isolate the unit under test by simulating dependencies and controlling their behavior.
BTo improve the performance of the application in production.
CTo replace the need for writing actual test cases.
DTo automatically generate test data for integration tests.
Attempts:
2 left
💡 Hint
Think about why we want to control external parts when testing a single unit.
framework
expert
3: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));
    }
}
A"one"
B"two"
CIndexOutOfBoundsException
DNullPointerException
Attempts:
2 left
💡 Hint
Remember that spy calls real methods unless stubbed.