Using Moco to Mock HTTP Services for API Testing and Frontend Development
This article introduces Moco, a Java‑based mock server tool, explains its purpose for early API testing and frontend development, provides step‑by‑step startup commands, showcases JSON request/response demos for various HTTP methods, outlines global configuration, and discusses its limitations.
Moco is a lightweight tool for building mock HTTP servers; it can run in API mode within test frameworks such as JUnit or as a standalone jar, allowing developers to simulate endpoints without implementing the actual backend.
Its primary uses are to enable mock‑based testing when backend interfaces are incomplete and to let frontend developers (both web and mobile) build and test UI components against simulated APIs.
In typical projects, API documentation is produced before implementation; Moco lets teams create a mock server after the documentation is available, so test cases can be executed and debugged even before the real services are ready, and later switch to the real endpoints.
To start Moco, download moco-runner-0.12.0-standalone.jar from the GitHub repository (https://github.com/dreamhead/moco) and run it via the command line, e.g., java -jar moco-runner-0.12.0-standalone.jar http -p 8080 . The protocol is usually http , and you can provide -c for a single JSON configuration file or -g for a global configuration file.
Below are common request demos expressed in JSON configuration files:
GET request examples:
[ { "description":"GET without parameters", "request":{"uri":"/withGetDemo","method":"get"}, "response":{"text":"This is a GET request without parameters"} }, { "description":"GET with parameters p1 and p2", "request":{"uri":"/withGetDemoByParam","method":"get","queries":{"p1":"hh","p2":"good"}}, "response":{"text":"this is a get method with parameter"} } ]POST request examples:
[ { "description":"Simple POST", "request":{"uri":"/postDemo","method":"post"}, "response":{"text":"This is post request"} }, { "description":"POST with form parameters", "request":{"uri":"/postDemoWithParam","method":"post","forms":{"param1":"one","param2":"two"}}, "response":{"text":"this is post request with param"} } ]POST request with JSON body and cookies:
[ { "description":"POST with cookies", "request":{"uri":"/postDemoWithCookies","cookies":{"login":"true"},"json":{"name":"hi","age":"3"}}, "response":{"status":"200","json":{"name":"success","status":"1"}} } ]Request with custom headers:
[ { "description":"Request with header", "request":{"uri":"/withHeader","method":"post","headers":{"content-type":"application/json"},"json":{"name":"xiaoming","age":"18"}}, "response":{"json":{"message":"success","status":"1"}} } ]Redirect examples:
[ { "description":"Redirect to Baidu", "request":{"uri":"/redirect","method":"get"}, "redirectTo":"http://www.baidu.com" }, { "description":"Target of redirect", "request":{"uri":"/toRedirect"}, "response":{"text":"this is the redirect page"} }, { "description":"Redirect to internal page", "request":{"uri":"/myStation"}, "redirectTo":"/toRedirect" } ]A global configuration file can include multiple JSON files and define a root path for each API group, for example:
[ { "file_root":"api/book", "include":"book.json" }, { "file_root":"api/person", "include":"person.json" } ]While Moco is easy to use and configure, it only supports simple stubbing; it cannot perform complex logic such as database queries, calculations, or other processing, so its suitability depends on whether a simple mock server meets the project’s needs.
360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.