Backend Development 8 min read

Using Moco to Mock HTTP Services: Step‑by‑Step Examples and JMeter Integration

This tutorial explains how to download the Moco‑runner JAR, configure various mock scenarios—including GET, POST, parameterized requests, cookies, headers, and redirects—and run them from the command line or through JMeter for comprehensive API testing.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Using Moco to Mock HTTP Services: Step‑by‑Step Examples and JMeter Integration

In interface testing, objects that are hard to construct or obtain can be replaced by virtual objects (mocks) to simplify testing. This guide demonstrates how to use the Moco library (moco‑runner‑0.11.0‑standalone.jar) to create such mock services.

Download : The JAR can be obtained from https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.0/ .

Running Moco : Start the mock server with java -jar ./moco-runner-0.11.0-standalone.jar http -p 8899 -c firstMock.json

1. Simple GET mock (firstMock.json):

[
  {
    "description": "这是我们第一个mock实例",
    "request": {"uri": "/mockdemo"},
    "response": {"text": "第一个mock框架demo"}
  }
]

Access http://localhost:8899/mockdemo to see the response.

2. GET without parameters (getMock/NoParameters):

[
  {
    "description": "模拟一个没有参数的get-mock",
    "request": {"uri": "/getMock/NoParameters", "method": "get"},
    "response": {"text": "这是一个没有参数的get-mock"}
  }
]

Visit http://localhost:8899/getMock/NoParameters for the result.

3. GET with query parameters (haveParameters):

[
  {
    "description": "模拟有参数get-mock请求",
    "request": {
      "uri": "/haveParameters",
      "method": "get",
      "queries": {"name": "大刀王五", "profession": "江湖侠客"}
    },
    "response": {"text": "我大刀王五的刀,那是有39米的,允许你先跑40米!"}
  }
]

Access http://localhost:8899/get/with/Parameters?name=大刀王五&profession=江湖侠客 .

4. POST without parameters (JMeter demo) :

[
  {
    "description": "模拟一个没有参数的post_mock请求",
    "request": {"uri": "/postMockNoparameter", "method": "post"},
    "response": {"text": "这是一个没有参数的post-mock"}
  }
]

Use JMeter to create a Thread Group, add an HTTP Request sampler pointing to /postMockNoparameter , and run the mock with java -jar ./moco-runner-0.11.0-standalone.jar http -p 8899 -c firstPostMock.json .

5. POST with form parameters (JMeter demo) :

[
  {
    "description": "模拟有参数的post_mock请求",
    "request": {
      "uri": "/postMockHaveparameter",
      "method": "post",
      "forms": {"name": "大刀王五", "profession": "江湖侠客"}
    },
    "response": {"text": "我王五带着参数入场"}
  }
]

Configure JMeter HTTP Request with the same form data and start the mock with the same command as above, using firstPostHaveMock.json .

6. GET with cookies :

[
  {
    "description": "模拟一个携带cookies请求头mock",
    "request": {"uri": "/cookiesMock", "method": "get", "cookies": {"login": "true"}},
    "response": {"text": "这是一个携带cookies的mock请求"}
  }
]

Add an HTTP Cookie Manager in JMeter, then run the mock with java -jar ./moco-runner-0.11.0-standalone.jar http -p 8899 -c firstCookiesGetMock.json .

7. POST with cookies and JSON body :

[
  {
    "description": "模拟一个带cookies-post请求,并返回一个json",
    "request": {
      "uri": "/cookiesPost",
      "method": "post",
      "cookies": {"login": "true"},
      "json": {"name": "测试", "profession": "测试"}
    },
    "response": {"status": 200, "json": {"qqq": "success", "msg": "大刀王五已就位!!"}}
  }
]

Configure JMeter with a HTTP Cookie Manager and a JSON body, then start the mock similarly.

8. GET with custom headers and JSON body :

[
  {
    "description": "模拟带headers-get的mock请求",
    "request": {
      "uri": "/GetHeader",
      "method": "get",
      "headers": {"content-Type": "application/json"},
      "json": {"name": "李四", "age": 18}
    },
    "response": {"json": {"name": "lisi", "status": 200}}
  }
]

In JMeter, add an HTTP Header Manager with the specified header before sending the request.

9. External redirect (to Baidu) :

[
  {
    "description": "模拟重定向到百度",
    "request": {"uri": "/redirectMock", "method": "get"},
    "redirectTo": "https://www.baidu.com"
  }
]

Run with java -jar ./moco-runner-0.11.0-standalone.jar http -p 8899 -c redirect.json and access http://localhost:8899/redirectMock to be redirected to Baidu.

10. Internal redirect (to another mock path) :

[
  {
    "description": "模拟重定向到自己指定路径Mock",
    "request": {"uri": "/redirect/Myself", "method": "get"},
    "redirectTo": "/redirect/new"
  },
  {
    "description": "这是被重定向到的请求",
    "request": {"uri": "/redirect/new"},
    "response": {"json": {"name": "王武", "age": "28"}}
  }
]

Start with java -jar ./moco-runner-0.11.0-standalone.jar http -p 8899 -c redirectMyselfMock.json and the request to /redirect/Myself will internally forward to /redirect/new .

All examples can be visualized with JMeter’s “View Results Tree” listener to verify the mock responses.

JavaJMeterMockMocoAPI testing
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.