What if you could test complex code without building everything it depends on by hand?
Why Mockito dependency setup in JUnit? - Purpose & Use Cases
Imagine you have a big software project with many parts that depend on each other. You want to test one part, but it needs other parts to work first. Doing this by hand means you have to create all those parts yourself, which takes a lot of time and effort.
Manually creating all the dependent parts is slow and boring. You might make mistakes or forget something, causing your tests to fail for the wrong reasons. This makes testing frustrating and unreliable.
Mockito helps by automatically creating fake versions of the parts your code depends on. These fakes behave like the real ones but are easy to control and check. This way, you can focus on testing just the part you want without building everything else.
RealService service = new RealService(new RealDependency()); service.performAction();
@Mock Dependency mockDependency; @InjectMocks Service service; service.performAction();
Mockito dependency setup lets you write fast, clear, and reliable tests by easily managing all the parts your code needs.
Testing a payment system where the payment processor depends on a bank API: Mockito creates a fake bank API so you can test payments without calling the real bank every time.
Manual setup of dependencies is slow and error-prone.
Mockito creates easy-to-control fake dependencies automatically.
This makes tests faster, simpler, and more reliable.
Practice
Solution
Step 1: Understand Mockito's role
Mockito is a tool used to create mock objects that simulate real objects in tests.Step 2: Identify the purpose in testing
Using mocks helps isolate the code under test by replacing dependencies with fake versions.Final Answer:
To create fake objects for testing without using real implementations -> Option BQuick Check:
Mockito = mock objects for testing [OK]
- Thinking Mockito improves production performance
- Confusing Mockito with UI tools
- Assuming Mockito deploys applications
Solution
Step 1: Check groupId and artifactId
The correct groupId is 'org.mockito' and artifactId is 'mockito-core' for Mockito library.Step 2: Verify version and scope
Version 4.11.0 is correct and scope should be 'test' to use Mockito only during testing.Final Answer:
Correct Maven dependency with version 4.11.0 and test scope -> Option AQuick Check:
Maven dependency with test scope = org.mockito mockito-core 4.11.0 test [OK]
- Omitting the test scope
- Using wrong artifactId like 'mockito-all'
- Using incorrect or too new version
Solution
Step 1: Understand Gradle scopes
testImplementation means the dependency is available only during test compilation and runtime.Step 2: Choose correct scope for Mockito
Mockito is used only in tests, so testImplementation is the correct scope.Final Answer:
testImplementation 'org.mockito:mockito-core:4.11.0' -> Option AQuick Check:
Mockito test dependency = testImplementation [OK]
- Using implementation instead of testImplementation
- Using compileOnly which excludes runtime
- Using runtimeOnly which excludes compile time
System.out.println(mockService.getData());?
import static org.mockito.Mockito.*;
public class ServiceTest {
@Test
void testGetData() {
DataService mockService = mock(DataService.class);
when(mockService.getData()).thenReturn("Mocked Data");
System.out.println(mockService.getData());
}
}Solution
Step 1: Understand mock behavior
The mock object returns what is specified in when().thenReturn(), here "Mocked Data".Step 2: Check the printed output
Calling getData() on mockService returns "Mocked Data", so it prints that string.Final Answer:
Mocked Data -> Option DQuick Check:
Mockito when().thenReturn() sets output = Mocked Data [OK]
- Expecting null because it's a mock
- Assuming real method runs
- Thinking it throws an exception
import static org.mockito.Mockito.*;
public class UserServiceTest {
@Mock
UserRepository repo;
@Test
void testUser() {
when(repo.findUser()).thenReturn(new User("Alice"));
UserService service = new UserService(repo);
assertEquals("Alice", service.getUserName());
}
}Solution
Step 1: Check @Mock initialization
@Mock fields need initialization, usually by calling MockitoAnnotations.openMocks(this) in setup.Step 2: Verify test setup completeness
Without initialization, repo remains null causing NullPointerException when used.Final Answer:
Missing MockitoAnnotations.openMocks(this) to initialize @Mock fields -> Option CQuick Check:
@Mock requires initialization = Missing MockitoAnnotations.openMocks(this) to initialize @Mock fields [OK]
- Forgetting to initialize @Mock fields
- Assuming @Mock works without setup
- Mixing method names without checking
Ready to prove it?
Pass the test and this pattern moves unknown
