What is the purpose of the @Mock annotation in JUnit testing?
The @Mock annotation is used to create mock objects. These are fake versions of real objects that simulate behavior for testing without using the actual implementation.
Click to reveal answer
intermediate
How do you initialize mocks annotated with @Mock in a JUnit 5 test class?
You initialize mocks by calling MockitoAnnotations.openMocks(this) in a setup method annotated with @BeforeEach. This prepares the mock objects before each test runs.
Click to reveal answer
intermediate
What is the difference between a mock and a spy in testing?
A mock simulates an object and does not call real methods unless specified. A spy wraps a real object and calls real methods unless stubbed to do otherwise.
Click to reveal answer
beginner
Can you use @Mock without a mocking framework like Mockito?
No, @Mock is part of Mockito and requires it to create mock objects. Without Mockito or a similar framework, @Mock has no effect.
Click to reveal answer
beginner
Why is mocking useful in unit testing?
Mocking helps isolate the unit being tested by replacing dependencies with controlled fake objects. This makes tests faster, more reliable, and focused on the unit's behavior.
Click to reveal answer
What does the @Mock annotation do in a JUnit test?
AMarks a test method to be ignored
BRuns the actual method implementation
CInitializes the test class
DCreates a fake object to simulate behavior
✗ Incorrect
@Mock creates a fake object to simulate behavior for testing.
How do you activate mocks annotated with @Mock in JUnit 5?
The call to MockitoAnnotations.openMocks(this) initializes the @Mock annotated fields.
Step 2: Analyze the stubbed method behavior
The line when(service.getData()).thenReturn("mocked data") sets the mock to return "mocked data" when getData() is called.
Final Answer:
mocked data -> Option D
Quick Check:
Stubbed mock method returns set value [OK]
Hint: Initialize mocks and stub methods to control output [OK]
Common Mistakes:
Forgetting to initialize mocks before use
Expecting real method output instead of stubbed
Confusing null return with stubbed return
4. Identify the error in the following JUnit 5 test code using @Mock:
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
class UserServiceTest {
@Mock
UserRepository repo;
@Test
void testFindUser() {
when(repo.findUserById(1)).thenReturn(new User(1, "Alice"));
UserService service = new UserService(repo);
User user = service.findUser(1);
assertEquals("Alice", user.getName());
}
}
medium
A. The test method is missing the @Mock annotation
B. The when method is used incorrectly
C. Mocks are not initialized before use
D. UserService constructor cannot accept mocks
Solution
Step 1: Check mock initialization
The code declares @Mock UserRepository repo; but never initializes mocks with MockitoAnnotations.openMocks(this) or similar.
Step 2: Understand consequence of missing initialization
Without initialization, repo is null, so calling when(repo.findUserById(1)) causes a NullPointerException.
Final Answer:
Mocks are not initialized before use -> Option C
Quick Check:
Always initialize mocks before using them [OK]
Hint: Always initialize mocks before calling methods on them [OK]
Common Mistakes:
Forgetting MockitoAnnotations.openMocks(this)
Assuming @Mock auto-initializes mocks
Misusing when() without initialized mocks
5. You want to test a service method that depends on two different collaborators. You use @Mock to create fake objects for both. Which of the following is the best practice to ensure your mocks work correctly in the test?
hard
A. Declare mocks as local variables inside test methods
B. Initialize mocks with MockitoAnnotations.openMocks(this) in a setup method
C. Use real instances instead of mocks for better accuracy
D. Manually instantiate mocks with new keyword
Solution
Step 1: Understand mock initialization best practice
Mocks annotated with @Mock must be initialized before use, typically by calling MockitoAnnotations.openMocks(this) in a setup method.
Step 2: Evaluate other options
Manually creating mocks with new is incorrect; real instances break isolation; local variables lose scope and initialization benefits.
Final Answer:
Initialize mocks with MockitoAnnotations.openMocks(this) in a setup method -> Option B
Quick Check:
Initialize mocks in setup for reliable tests [OK]
Hint: Initialize all @Mock fields in setup method [OK]
Common Mistakes:
Creating mocks manually with new keyword
Using real objects instead of mocks
Declaring mocks inside test methods without initialization
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.