To build a common mock server system for functional testing

Source: Internet
Author: User

Mocks are already well known in unit tests. Today we have a variety of powerful and useful mock frameworks that can easily unlock the various dependencies in unit testing, which greatly reduces the difficulty of writing unit tests. Test-driven Development (TDD) further takes mocks as a design tool to assist in identifying interfaces and responsibilities for interaction between elements.

So is it necessary to use a mock at this level of functional testing, which refers to the user-level test? And how will it be constructed if necessary? Or is it possible to build a generic mock server system like a unit test? Based on my practical experience, this article will introduce you to a common mock server system's main components and design ideas.

Why

Today's business systems are rarely isolated, and they are more or less in need of services provided by a sibling team or other company, which is causing trouble for our reconciliation tests. In this case, our common solution is to build a temporary server, simulate those services, and provide data for the reconciliation test. This is the prototype of mock server. In general, it is simpler to build this mock server system, but it is also relatively simple and often requires repeated development for different interfaces. Is it possible to build a common mock server system like the mock framework used in unit tests?

How

Looking at the mock framework in unit tests, we find that the process of using mocks in general is:

init mock  //Create mock Object
config mock//set mock expect
setup mock///Set mock object to object called
//call to test interface, The code in the tested interface invokes the Mock object
verify//Authentication
Mockito Example:
user expected = new User ("admin", "12345");
init
Userdao dao = mock (userdao.class);
Config when
(dao.findbyname ("admin")). Thenreturn (expected);
Setup
userservice service = new UserService (dao);
Call
User actual = Service.login ("admin", "12345");
Verify or Assert

With this approach, I can build a simple mock server system, and in the next section, we'll evolve a more complete version of the mock server.

Version 1 (simple analog value)

Let's say we need to mock the HTTP interface. Our mock server provides a configuration interface (corresponding to the Config mock step above), which invokes the configuration interface before the test run to pass the HTTP interface URL that needs to be mock, and the value that needs to be returned, to the mock server to establish an association of URLs to return values ( Here is similar to the existence of a hash table. Mock server also provides another interface for system calls to be tested. This is a generic interface where all the addresses that originally point to the real service are all directed to the interface (either by modifying the configuration or modifying the system Hosts file). When the interface is invoked, it looks for an association of the URL just established to the return value and returns the mock value. Such a very simple mock server is built, and for some simple cascading scenarios, it's basically enough. The schematic diagram of our mock server is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.