In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what is Mock testing". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
First acquaintance of mock
As a verb, mock means to imitate; as a noun, mock is a simulated object that can imitate the behavior of real objects.
What is the object that mock simulates in software testing?
It must not be the object we are testing, but the dependency of SUT. In other words, the role of mock is to simulate the behavior of SUT dependent objects.
The object to be tested is generally called SUT (Software Under Test)
The text is difficult to understand, let's draw a picture, as shown in the following figure, the tested object is A _ Magi A, B ~ B depends on C. And what we want to mock is the behavior of B. An in the picture is SUT.
Why do you need to simulate the behavior of B?
(1) improve the test coverage of A. A depends on B and essentially depends on the return result of B, that is to say, the return result of B will affect the behavior of A. Through mock B, we can construct all kinds of normal and abnormal return results from B, so as to test A's behavior more fully.
(2) to avoid the factors of B and thus have an impact on A. There may be many problems with relying on real B to test A: B cannot be tested when the development of B is not completed. B cannot be tested when B has obstructive bug. B cannot be tested when C has obstructive bug.
(3) improve the testing efficiency of A. The real behavior of B may be slow, while the simulation behavior of B is very fast, so it can speed up the test execution of A.
Mock race
Common mock types are shown in the following figure:
Explain it from the bottom up:
(1) the object of the method-level mock:mock is a function call, such as getting a system environment variable.
(2) an object at the class level mock:mock is a class, such as a HTTP server.
(3) the object of interface level mock:mock is an API interface.
(4) the object of service level mock:mock is the whole service. For example, when the front-end engineers test themselves, they can say that the entire service of the backend is mock-disabled, which is tantamount to mock all the interfaces of the backend.
Five ways of interface mock injection
When using mock for interface testing, it is generally necessary to do two things, namely pile driving and pile adjustment.
In fact, piling is to create a mock pile and specify the content of the API request and its mapped mock response; the so-called piling is that the service under test requests the mock pile and receives the mock response.
In fact, there is a hidden but very important thing between piling and pile adjustment, and that is the mock injection of mock piles.
What is mock injection?
The essence of mock is to replace real dependencies with mock piles. The so-called mock pile injection is the process of blocking the link between the tested service and the real service and establishing the link between the tested service and the mock.
How to inject mock?
Generally speaking, the injection mode of mock pile is related to the architecture, the architecture of the service under test and other factors. In practice, the common ways of mock pile injection include, but are not limited to, the following five.
(1) API request construction
In the mock interface, the service under test is the requester of the API, that is, the client; the dependent service is the responder of the API, that is, the server. According to where mock works, mock can be divided into client-side mock and server-side mock.
The client mock:mock works inside the service under test and directly intercepts the API request method (such as the HTTP Client method) of the service under test. When the service under test invokes the API request method, it directly returns the predefined mock response from inside the method.
The server mock:mock works outside the service under test. As a HTTP server, it receives the API request sent by the service under test and returns a predefined mock response.
In fact, the injection of client-side mock is to modify the API request method of the service under test, that is, to add mock processing logic to the API request method. Mock branches are executed when certain conditions are met, and real branches are executed when they are not met.
It can be realized in two ways, one is to directly modify the source code, and the other is to use bytecode enhancement technology to transform the bytecode (Java language).
The injection method of API request modification is suitable for client-side mock. Its advantage is excellent performance, but its disadvantage is that the implementation cost is high.
(2) Local configuration
For server-side mock, a unique mock pile address is generated after piling. If the service under test wants to call this pile, it needs to know the address of the pile. How to let the service under test know the address of the pile? One of the most direct methods is to provide a dependent service address configuration item for the service under test, and modify the dependent service address to a mock address when you need to use mock.
The advantage of local configuration is that it is simple to implement, but the disadvantage is that the service under test needs to be restarted to modify the configuration item, so it is not convenient to switch between mock service and real service.
(3) configuration Center
In the server mock, in order to avoid restarting the tested service caused by modifying the configuration item of the dependent service address, a configuration center (such as Spring Cloud Config Server) can be used to store and manage the configuration of the dependent service address, or a registry (such as Spring Cloud Eureka) can be used to record the mapping between the service and the service address.
When using a configuration or registry, the method of mock injection is to modify the configuration center to change the dependent service address to the mock address. This injection method does not require a restart of the service under test, but there can be a delay from the configuration change to the configuration taking effect.
(4) reverse proxy
In the micro-service architecture, the service under test and the dependent service may not be directly connected, but through a layer of reverse proxy, such as API gateway. In this case, the service under test invokes the service-dependent interface indirectly by calling the API gateway.
In API gateway mode, the specific method of mock injection is to modify the API gateway configuration and change the address bound by the service API gateway interface to the mock address.
The advantage of this injection is that it has no intrusion into the service under test and implements a finer-grained (interface-level) mock. Of course, depending on the implementation of the API gateway, there may still be a certain delay. Amazon AWS's API gateway uses this approach for mock.
(5) forward agent
In addition to acting as a HTTP server, the server mock can also have the function of a HTTP agent. This architecture is also known as a mock agent, such as mock server proxy. For the mock agent, it can not only return the mock response, but also forward the API request to the dependent service when needed, and return the real response of the dependent service to the service under test.
Using the forward proxy mode, mock injection is used to modify the dependent address or network proxy of the service under test to mock address. This injection method needs to restart the service under test, and its advantage is that it can achieve fine-grained mock and automatically generate mock according to the recorded real response.
Comparison of five injection methods
A table to sum up
Two functions of mock that can not be ignored
What is often misunderstood about mock is that mock is just the result of the simulation.
In fact, mock can also provide two major functions: (1) recording the real call information; (2) generating simulated return information.
For test cases, we need to care not only whether mock returns the desired results, but also whether SUT calls the mock object in the desired way.
If SUT is not called as expected, for example, no parameters are passed or the parameters are incorrect, then SUT has a problem.
Mock needs to record the call information from SUT in detail and provide it to the use case for verification. For example, Java mockito provides this kind of verification feature:
List mockedList = mock (MyList.class); mockedList.size (); / / verify the size function call and only call verify (mockedList, times (1)) .size ()
Common mock tools
Unit test level
This level of mock tools include easymock, jMock, Mockito, Unitils Mock, PowerMock, JMockit and so on. About their respective advantages and disadvantages, you can query them online.
Interface test level
The main function of the interface-level mock tool is to respond to a user's request and simulate server to return the response data of an interface. The commonly used ones are:
Wiremock
Mockserver
Moco
Mock.js
RAP
Mock is not a silver bullet.
Having talked so much about the advantages of mock, mock actually has many disadvantages, such as:
(1) mock may cause problems to be omitted. There may be GAP in the simulated behavior and real behavior of mock, which causes the test based on mock to pass, but the test based on real object to fail, which means that the problem is omitted. It is difficult for mock to simulate all the real situations.
(2) mock brings high maintenance cost. The structure of the test case based on mock is complex, and it is not easy to implement and maintain. Later, when the code under test changes, it needs to adapt the mock code.
To put it simply: mock is not a silver bullet.
A summary with an attitude
Mock is not a silver bullet. Mock has its advantages and disadvantages. A picture can be summarized as follows:
Having said so much, how to use mock correctly at work? Here are two suggestions. Knock on the blackboard.
(1) do not overuse mock. Master the degree of using mock in the test case. Mock is preferred for system-level calls involving network access, database reading and writing, operating system interaction, etc.
(2) Don't rely too much on mock-based test results. No matter how adequate mock-based testing is, there is no guarantee that problems will not be left out. A complete testing strategy must be composed of mock-based testing and non-mock-based testing, both of which complement each other.
This is the end of the content of what is the Mock Test. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.