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.

Huawei Cloud Developer Alliance
Huawei Cloud Developer Alliance
Huawei Cloud Developer Alliance
Mastering Mockito: Simplify Java Unit Tests with Powerful Mocking

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

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javamavenunit testingMockingMockitoScala
Huawei Cloud Developer Alliance
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.