Mockito helps us test code by creating fake objects. Setting it up means adding the right tools to your project so you can use Mockito easily.
When you want to test a class that uses other classes without running the real ones.
When you need to check how your code behaves with different responses from other parts.
When you want to write tests faster by avoiding complex setup of real objects.
Syntax
JUnit
For Maven projects, add this to your pom.xml:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
For Gradle projects, add this to your build.gradle:
testImplementation 'org.mockito:mockito-core:5.4.0'
Use the latest stable version of Mockito for best features and fixes.
Scope 'test' means Mockito is only used during testing, not in the final app.
This test shows Mockito working after setup. It creates a fake list, tells it to return 3 when size() is called, and checks that it does.
JUnit
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class MockitoSetupTest {
@Test
public void testMockList() {
// Create a mock List
List<String> mockedList = mock(List.class);
// Use the mock object
when(mockedList.size()).thenReturn(3);
// Verify the size method returns 3
assertEquals(3, mockedList.size());
}
}
OutputSuccess
Important Notes
Make sure your test framework (like JUnit 5) is also set up correctly.
Mockito works best with Java 8 or higher.
Summary
Mockito dependency setup means adding Mockito to your project files.
This setup lets you create fake objects for easier testing.
Use the correct version and scope for smooth testing.
Practice
(1/5)
1. What is the main purpose of setting up Mockito as a dependency in a JUnit project?
easy
A. To automatically generate user interfaces
B. To create fake objects for testing without using real implementations
C. To improve the performance of the application in production
D. To deploy the application to a server
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 B
Quick Check:
Mockito = mock objects for testing [OK]
Hint: Mockito creates fake objects to test code easily [OK]
Common Mistakes:
Thinking Mockito improves production performance
Confusing Mockito with UI tools
Assuming Mockito deploys applications
2. Which of the following is the correct Maven dependency snippet to add Mockito version 4.11.0 for test scope?
easy
A.
org.mockito
mockito-core
4.11.0
test
B.
org.mockito
mockito-core
4.11.0
C.
org.mockito
mockito-all
3.0.0
compile
D.
org.mockito
mockito-core
5.0.0
test
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 A
Quick Check:
Maven dependency with test scope =
org.mockito
mockito-core
4.11.0
test
[OK]
Hint: Use 'mockito-core' with test scope and correct version [OK]
Common Mistakes:
Omitting the test scope
Using wrong artifactId like 'mockito-all'
Using incorrect or too new version
3. You want to use Mockito in a JUnit 5 project with Gradle. Which of the following Gradle dependency setups correctly adds Mockito version 4.11.0 only for test implementation?
easy
A. testImplementation 'org.mockito:mockito-core:4.11.0'
B. implementation 'org.mockito:mockito-core:4.11.0'
C. compileOnly 'org.mockito:mockito-core:4.11.0'
D. runtimeOnly 'org.mockito:mockito-core:4.11.0'
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 A
Quick Check:
Mockito test dependency = testImplementation [OK]
Hint: Use testImplementation for test-only dependencies in Gradle [OK]
Common Mistakes:
Using implementation instead of testImplementation
Using compileOnly which excludes runtime
Using runtimeOnly which excludes compile time
4. Given the following JUnit test code snippet using Mockito, what will be the output of 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());
}
}
medium
A. Original data from DataService
B. null
C. Throws NullPointerException
D. Mocked Data
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 D
Quick Check:
Mockito when().thenReturn() sets output = Mocked Data [OK]