Mastering Mockito: Simplify Java Unit Tests with Powerful Mocking
This article introduces Mockito, a Java mocking framework, explains when to use it for testing code with external dependencies, and provides step‑by‑step guidance—including Maven setup, creating mock objects in Scala, and configuring mock behavior—complete with code snippets and reference links.
What is Mockito?
Mockito is a powerful Java mocking framework that allows developers to create and configure mock objects, simplifying testing of classes with external dependencies by defining input‑output behavior without implementing the actual method logic.
When to Use Mockito
It is suitable for unit testing methods that depend on external services such as REST APIs, client‑server calls, or database queries, where constructing real dependencies would be cumbersome.
How to Use Mockito
Add Dependency Maven dependency:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>Create Mock Objects In Scala you can only mock companion objects or classes, not plain objects. Use Mockito.mock(classOf[...]) to create a mock.
class VpcClient {
def getRouteTable(projectId: String, token: String, url: String): Seq[RouteTable] = {
val header = Map(RequestAttributes.X_AUTH_TOKEN -> token,
"Content-Type" -> MediaType.APPLICATION_JSON)
val response = restClient.get(url, header)
}
}
object VpcClient {
private lazy val _instance: VpcClient = new VpcClient(conf)
private var mockClient: VpcClient = _
def getInstance(): VpcClient = {
if (RuntimeEnvironment.isTesting && null != mockClient) {
return mockClient
}
_instance
}
def setMockClient(vpcClient: VpcClient): Unit = {
mockClient = vpcClient
}
}Configure Mock Behavior Use Mockito.doReturn(...).when(mock).method(args) to specify the return value for a given input.
Mockito.doReturn(routeTableInfo)
.when(vpcClient).getRouteTable(projectId, token, url)Summary
The article outlines Mockito’s use cases, demonstrates how to add the Maven dependency, create mock objects in Scala, and configure their behavior, noting minor differences from Java usage.
References
Official site: http://mockito.org
API documentation: http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html
Source code: https://github.com/mockito/mockito
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Huawei Cloud Developer Alliance
The Huawei Cloud Developer Alliance creates a tech sharing platform for developers and partners, gathering Huawei Cloud product knowledge, event updates, expert talks, and more. Together we continuously innovate to build the cloud foundation of an intelligent world.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
