Abstract:Every programmer writing Unit Test has a few headaches: how to Test a REST interface; How to test a complex method involving the client calling the server; How to test a complex method that involves reading data from a database… Mockito can help us solve these problems easily. Mockito is what? Mockito is a powerful simulation testing framework for Java development, through Mockito we can create and configure the Mock object, and simplified outside…

This article is from Mockito — a powerful tool to make unit testing easy for you, by data Lake enthusiasts.

Every programmer writing Unit Test has a few headaches: how to Test a REST interface; How to test a complex method involving the client calling the server; How to test a complex method that involves reading data from a database… Mockito can help us solve these problems easily.

Mockito is what?

Mockito is a powerful mock-testing framework for Java development. Mockito allows you to create and configure Mock objects to simplify testing classes that have external dependencies. In short, after creating a mock object, we don’t care how the methods in the object are implemented, we just need to define an input and an output for the method.

When to use Mockito:

Suppose we want to test Method A, which relies on Method B, Method C, and Method D, which are difficult to build (such as ObsClient needs real AK SK, HttpClient needs to build client and server, The Database is relatively easy to build, but if Method C is just a combined query from Table1 and Table2, you have to insert data into Table1 and Table2 separately, which is very complicated), consider Mockito for elegant testing. That’s a bit of a miss

Use Mockito:

1. Import dependence

Maven dependencies are as follows:

< the dependency > < groupId > org. Mockito < / groupId > < artifactId > mockito - core < / artifactId > < version > 1.10.19 < / version > <scope>test</scope> </dependency>

Import org.mockito.Mockito

Create Mock objects

It is important to note that in Scala you can only mock objects for companion objects or classes, not directly for objects. Mockito.mock(classOf[… ) to create mock objects

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 = { // Keep original logic for other ut, which didn't set mock client. if (RuntimeEnvironment.isTesting && null ! = mockClient) { return mockClient } _instance } // only used for UT def setMockClient(vpcClient: VpcClient): Unit = { mockClient = vpcClient } }

For example, you can now mock VpcClient to call the getRouteTableList method in VpcClient.

class UtilSuite {
  val vpcClient = Mockito.mock(classOf[VpcClient])

VpcClient.setMockClient(vpcClient)
}

3. Configure Mock objects

Mockito.doReturn(routeTableInfo).when(vpcClient).getRouteTable(projectId, token,url)

When we have a Mock object, we can call the object’s methods and use mockito.doreturn ().when().method to set the output to be returned on the input when the method is called. The input must be consistent with the actual input parameters of the method, and the output must be consistent with the actual return parameters of the method.

class UtilSuite {
  val vpcClient = Mockito.mock(classOf[VpcClient])

VpcClient.setMockClient(vpcClient)


val projectId = "projectId"

val token = "token"

val url= "url"

val routeTableInfo = new RouteTableInfo
 
Mockito.doReturn(routeTableInfo).when(vpcClient).getRouteTable(projectId, token, url)
}

Conclusion:

This explains how Mockito can be used and how to create mock objects using Mockito in Scala. Mockito Mockito Mockito Mockito Mockito Mockito Mockito Mockito Mockito Mockito Mockito Mockito

  • Website: http://mockito.org
  • The API documentation: http://docs.mockito.googlecod…
  • The project source code: https://github.com/mockito/mo…

For more AI related data, algorithms, models and other AI assets, please click “Learn more”, AI Gallery wait for you!

Click follow to learn about the fresh technologies of Huawei Cloud