Abstract: believes that every programmer will encounter some headaches when writing Unit Test: how to test a rest interface; how to test a complex method that includes client calls to the server; how to test one that includes reading from the database Complex methods of fetching data. . . Mockito can help us solve these problems easily. What is Mockito? Mockito is a powerful mock test framework for Java development. Through Mockito we can create and configure Mock objects, thereby simplifying external...
This article is shared from Huawei Cloud Community " mockito-a powerful tool for unit testing ", author: data lake enthusiast.
I believe that every programmer will encounter some headaches when writing Unit Test: how to test a rest interface; how to test a complex method that includes a client calling a server; how to test a complex method that includes reading data from a database method. . . Mockito can help us solve these problems easily.
What is Mockito?
Mockito is a powerful mock test framework for Java development. Through Mockito we can create and configure Mock objects, thereby simplifying the testing of classes with external dependencies. Simply put, after creating the mock object, you don't need to care about how the methods in this object are implemented. We only need to define an input and output for this method.
When to use Mockito:
Suppose now that you want to test method A, method A relies on Method B, Method C, and Method D, and these 3 methods are not easy to build (such as ObsClient needs real AK SK, HttpClient needs to build client and server, Database Relatively easy to build, but assuming that Method C is only a joint query from table1 and table2, you have to insert data to table1 and table2 respectively, which is very cumbersome). At this time, you can consider Mockito for elegant testing. Of course, if you want to build a real test scenario , It’s a little bit of a sacrifice~~~
Use of Mockito:
1. Introduce dependence
Maven depends on the following:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
In the code: import org.mockito.Mockito
2. Create Mock Object
It should be noted here that in scala, mock objects can only be created for companion objects or classes, and mock objects cannot be created directly for Object. Scala uses 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, the VpcClient is now mocked so that the getRouteTableList method in VpcClient can be called.
class UtilSuite {
val vpcClient = Mockito.mock(classOf[VpcClient])
VpcClient.setMockClient(vpcClient)
}
3. Configure the Mock object
Mockito.doReturn(routeTableInfo).when(vpcClient).getRouteTable(projectId, token,url)
When we have a Mock object, we can call the method of the object, and use Mockito.doReturn().when().method to set when the method is called, a certain output will be returned under certain input. The input here should be consistent with the real input parameters of the method, and the output should also be consistent with the real 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)
}
Summarize:
The above mentioned the usage scenarios of Mockito and how to use Mockito to create mock objects in scala. It is slightly different from using Mockito in java. If you are interested, you can refer to the following official documents:
- Official website: http://mockito.org
- API documentation: http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html
- Project source code: https://github.com/mockito/mockito
For more AI related data, algorithms, models and other AI assets, please click " learn more about ", AI Gallery is waiting for you!
Click to follow to learn about Huawei Cloud's fresh technology for the first time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。