0
0
JUnittesting~10 mins

Mockito dependency setup in JUnit - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if Mockito is correctly set up in a JUnit 5 project by verifying that a mock object is created and behaves as expected.

Test Code - JUnit 5 with Mockito
JUnit
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;

@ExtendWith(MockitoExtension.class)
public class MockitoSetupTest {

    @Mock
    private List<String> mockedList;

    @Test
    public void testMockCreation() {
        // Arrange
        when(mockedList.size()).thenReturn(3);

        // Act
        int size = mockedList.size();

        // Assert
        assertEquals(3, size, "Mocked list size should be 3");
    }
}
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test starts and JUnit initializes the test class with MockitoExtensionJUnit test runner is ready; MockitoExtension prepares mocks-PASS
2Mockito creates a mock instance of List<String> and injects it into mockedListmockedList is a Mockito mock object-PASS
3Test sets up behavior: when mockedList.size() is called, return 3mockedList configured to return 3 for size()-PASS
4Test calls mockedList.size()mockedList.size() returns 3 as configuredassertEquals(3, size)PASS
5Test finishes successfully with assertion passingTest passed, mock works as expectedassertEquals passedPASS
Failure Scenario
Failing Condition: MockitoExtension is not registered or Mockito dependency is missing, so mock is not created
Execution Trace Quiz - 3 Questions
Test your understanding
What does the @ExtendWith(MockitoExtension.class) annotation do in this test?
AIt disables Mockito for this test
BIt runs the test in a separate thread
CIt initializes Mockito mocks before each test
DIt sets the test timeout
Key Result
Always use @ExtendWith(MockitoExtension.class) in JUnit 5 tests to enable Mockito annotations and ensure mocks are properly created before tests run.