Unit testing-how to mock objects

Python combat community

Java combat community

Long press to identify the QR code below, add as required

Scan QR code to follow to add customer service

Enter the Python community▲

Scan QR code to follow to add customer service

Enter the Java community

Author丨Coder Technology Stack

Source丨Ingenuity Java (ID: code-the-world)

concept

The concept of mock is actually very simple: the so-called mock is to create a fake object of a class, which is used to replace the real object in the test environment to achieve two major goals:

  • Verify the calling of certain methods of this object, how many times are called, what are the parameters, etc.

  • Specify the behavior of certain methods of this object, return a specific value, or perform a specific action

The above two main purposes should be well understood. After understanding the purpose, you will roughly know when to use mock.

scenes to be used

  • The object structure that the tested object depends on is complex

For example: class A depends on class B, class B depends on class C and class D, class C depends on..., class D depends on... Here we want to test classA, and if there is no mock, we have to construct it according to requirements Objects such as classBCD are time-consuming and labor-intensive. In the case of mock, we can directly mockclassBCD and design its behavior to achieve the purpose of testing classA, because we just want to test whether the behavior of class A meets expectations, we don't need to test dependent objects.

  • The module that the tested unit depends on has not been developed yet, and the tested object needs to rely on the return value of the module for testing. That is, the test unit relies on downstream data that cannot be obtained

For example: the test of the method in the service requires access to the database operation in the dao and obtain its return value, but we have not developed the corresponding dao method yet, we can mock a dao layer object and set its behavior as: when calling it One of the methods returns a set value. In this way, we can test the service without being affected by the development speed of the dao layer. It is also consistent with only testing the service method logic.

in principle

  • It is not necessary to mock objects for all unit tests, only use mocks for the relevant scenarios introduced in the above usage scenarios.

  • In hierarchical testing, the high-level test design can be based on the following assumptions: the low-level test has guaranteed the quality of the underlying objects, and the high-level does not need to care about the internal logic quality of the low-level objects. In this case, high-level objects can mock low-level objects.

Common framework

  • EasyMock  : The early more popular mock framework, it provides a simulation of the interface, can complete the general test process through three steps of recording, playback, and inspection, can verify the type, number, and sequence of method calls, and can make the Mock object return to the specified Value or throw specified exception

  • Mockito : The popular mock tool after EasyMock. Compared with EasyMock, the learning cost is low, and it has a very concise API, and the test code is highly readable.

  • PowerMock : This tool is extended on EasyMock and Mockito. The purpose is to solve problems that EasyMock and Mockito cannot solve. For example, static, final, and private methods cannot be mocked.

In fact, the code with a well-designed test architecture generally does not need these functions, but if you add unit tests to an existing project and the old code has problems and cannot be changed, you have to use these functions. PowerMock uses the same API as the extended framework when extending its functions. Developers who are familiar with the simulation framework supported by PowerMock will find PowerMock very easy to use. The purpose of PowerMock is to implement additional functions by adding few methods and annotations to interfaces that are already familiar to everyone. Currently PowerMock only extends EasyMock and mockito, and it needs to be used in conjunction with EasyMock or Mockito.

  • Jmockit : JMockit is a lightweight mock framework that is a set of tools and APIs to help developers write test programs. The project is completely developed based on the java.lang.instrument package of Java 5 SE, and uses the ASM library internally to modify Java Bytecode.

Jmockit functions are similar to PowerMock, some functions are even more powerful, but I personally feel that the code is not very readable.

There are many mock frameworks. When we choose mock frameworks, we can use relative mock frameworks according to the usage environment. However, under normal circumstances, I personally feel that mockito is still good: the code is readable, easy to use, and most of the units of the project. Tests are sufficient, no need to rely on other components. This may be why mockito is so popular.

refer: blog blog

程序员专栏 扫码关注填加客服 长按识别下方二维码进群


Recommended recent exciting content:  

 Comparison of programmer income in China, the United States, Japan and India

 A sad day for programmers

 SringMVC from entry to source code, this one is enough

 10 Python visual animations, carefully and beautifully


Watch the good article here to share it with more people↓↓

Guess you like

Origin blog.csdn.net/Px01Ih8/article/details/109268599