How to Build and Run Mock Servers with Moco: A Hands‑On Guide

This tutorial walks through creating a MocoServer instance, configuring ports and log monitors, and using the moco API to define request‑response behavior, followed by a comprehensive Java utility class that simplifies server setup, monitoring, and lifecycle management for backend testing.

FunTester
FunTester
FunTester
How to Build and Run Mock Servers with Moco: A Hands‑On Guide

In the previous two videos the author introduced the moco API mock framework; this article continues with practical usage of the mocoserver object and the moco API.

The mocoserver class is simple; the most commonly used parameters are port and logmonitor, while other options are rarely needed.

The moco API demonstration is brief due to limited recording time; it shows how to configure request and response behavior, and readers are encouraged to try additional scenarios.

Example: Creating a Mock Server

package com.fun

import com.fun.base.bean.Result
import com.fun.moco.MocoServer
import com.github.dreamhead.moco.HttpServer

class DeleteNull extends MocoServer {
    public static void main(String[] args) {
        HttpServer server = getServer(8088, "1.log")
        server.get(urlMatcher("/abc+")).response("funtest")
        server.get(urlOnly("/ab")).response(obRes(Result.success(getJson("2323=3323"))))
        server.get(urlStartsWith("/test")).response(limit("fun", "tester", 3000))
        server.request(both(urlStartsWith("/te"), eqArgs("q", "fun"))).response(obRes(Result.success(getJson("name=fun"))))
        server.get(urlStartsWith("/error")).response(setStatus(404))
        server.response("haha")
        MocoServer drive = run(server)
        waitForKey("fan")
        drive.stop()
    }
}

MocoServer Utility Class

package com.fun.moco

import com.github.dreamhead.moco.HttpServer
import com.github.dreamhead.moco.MocoMonitor
import com.github.dreamhead.moco.MocoRequestHit
import com.github.dreamhead.moco.RequestHit
import com.github.dreamhead.moco.Runner

import static com.github.dreamhead.moco.Moco.httpServer
import static com.github.dreamhead.moco.Moco.log

/**
 * Utility class for obtaining HttpServer instances with logging and monitoring.
 */
class MocoServer extends MocoResponse {
    def array = []

    /** Get HttpServer on default port 12345 */
    static HttpServer getServer() {
        httpServer 12345, getLogMonitor()
    }

    /** Get HttpServer on specified port */
    static HttpServer getServer(int port) {
        httpServer port, getLogMonitor()
    }

    /** Get HttpServer with custom monitor */
    static HttpServer getServer(MocoMonitor mocoMonitors) {
        httpServer 12345, mocoMonitors
    }

    /** Get HttpServer with port, log file and monitor */
    static HttpServer getServer(final int port, String logName, MocoMonitor configs) {
        httpServer port, getLogMonitor(logName), configs
    }

    /** Get HttpServer with port and log file */
    static HttpServer getServer(final int port, String logName) {
        httpServer port, getLogMonitor(logName)
    }

    /** Create a file log monitor */
    static def getLogMonitor(String logName) {
        log LOG_Path + logName, DEFAULT_CHARSET
    }

    /** Default console log monitor */
    static def getLogMonitor() {
        log()
    }

    /** Counter monitor for verifying service start and request handling */
    static RequestHit getHitMonitor() {
        MocoRequestHit.requestHit()
    }

    /** Start all provided HttpServer instances */
    static MocoServer run(HttpServer... httpServer) {
        def server = new MocoServer()
        httpServer.each { x -> server.array << Runner.runner(x) }
        server.start()
    }

    /** Start the stored servers */
    def start() {
        array.each { x -> x.start() }
        this
    }

    /** Stop the stored servers */
    def stop() {
        array.each { x -> x.stop() }
    }
}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaautomationBackend testingHTTP serverMoCoAPI mocking
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

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.