0
0
JUnittesting~20 mins

@Mock annotation in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mockito Mock Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Purpose of @Mock Annotation in JUnit Tests
What is the main purpose of using the @Mock annotation in JUnit testing with Mockito?
ATo create a fake object that simulates the behavior of a real dependency for isolated testing.
BTo automatically generate test data for parameterized tests.
CTo mark a test method that should be ignored during test execution.
DTo inject real implementations of dependencies into the test class.
Attempts:
2 left
💡 Hint
Think about how you isolate the class under test from its dependencies.
Predict Output
intermediate
2:00remaining
Output of a Test Using @Mock Without Initialization
Consider this JUnit 5 test class using Mockito. What will happen when the test runs?
JUnit
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import static org.mockito.Mockito.*;

class ServiceTest {
    @Mock
    Repository repo;

    @Test
    void testService() {
        when(repo.getData()).thenReturn("mocked data");
        System.out.println(repo.getData());
    }
}
APrints 'mocked data' to the console.
BPrints 'null' to the console.
CThrows a NullPointerException at runtime.
DCompilation error due to missing import.
Attempts:
2 left
💡 Hint
Think about whether the @Mock annotation is activated automatically.
assertion
advanced
1:30remaining
Correct Assertion to Verify Mock Interaction
Given a mock object mockList of type List, which assertion correctly verifies that the add("item") method was called exactly once?
Averify(mockList).add("item"); assertTrue(true);
Bverify(mockList, times(1)).add("item");
CassertEquals(1, mockList.add("item"));
DassertThat(mockList).contains("item");
Attempts:
2 left
💡 Hint
Mockito provides a special method to check how many times a method was called.
🔧 Debug
advanced
2:00remaining
Why Does This Mockito Test Fail?
This test fails with a NullPointerException. What is the most likely cause?
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import static org.mockito.Mockito.*;

class UserServiceTest {
    @Mock
    UserRepository userRepository;

    @Test
    void testGetUser() {
        when(userRepository.findUserById(1)).thenReturn(new User(1, "Alice"));
        UserService service = new UserService(userRepository);
        User user = service.getUser(1);
        assertEquals("Alice", user.getName());
    }
}
AMockito was not initialized, so userRepository is null causing NullPointerException.
BThe User class does not have a constructor with parameters.
CThe when() method is called after the service is created, causing timing issues.
DThe assertEquals method is used incorrectly with parameters reversed.
Attempts:
2 left
💡 Hint
Check if the mock object is properly created before use.
framework
expert
2:30remaining
Best Way to Enable @Mock Annotation in JUnit 5 Tests
Which approach correctly enables the use of @Mock annotations in a JUnit 5 test class using Mockito?
AAdd <code>@MockBean</code> annotations instead of <code>@Mock</code>.
BCall <code>Mockito.mock()</code> manually for each mock field in every test method.
CUse <code>@RunWith(MockitoJUnitRunner.class)</code> on the test class.
DAnnotate the test class with <code>@ExtendWith(MockitoExtension.class)</code>.
Attempts:
2 left
💡 Hint
JUnit 5 uses extensions instead of runners.