Mockery - Lightweight API Mocking, Fault Injection & Proxy Engine

Mockery - Lightweight API Mocking, Fault Injection & Proxy Engine

Repository: https://codeberg.org/KOMOTO/Mockery


Building fast, reliable test automation pipelines and local dev setups shouldn't be held hostage by flaky staging environments, third-party API rate limits, or expensive sandbox charges.


Mockery is a lightweight, cross-platform CLI and engine built in .NET designed to solve the headaches of service virtualization, dynamic test data generation, and offline API mocking.


đź’ˇ Why I Built Mockery


As an SDET, I saw a recurring bottleneck in modern software delivery: teams spend more time managing broken test data and waiting on external APIs than actually validating software. Existing tools often fell into two extremes:

  1. Too simple: Hardcoded, static JSON mocks that break the moment you run tests in parallel.
  2. Too heavy: Enterprise service virtualization platforms that require complex cloud setups, database dependencies, and dedicated server costs.


I wanted to build a single, developer-friendly tool that acts as a Swiss Army knife for test environments—giving engineers the power to generate dynamic, realistic test data on the fly or record and replay live APIs completely offline.


🚀 What Mockery Does


Mockery operates across three distinct execution modes using a declarative, human-readable YAML specification:


1. Dynamic Mocking (Faker Engine & Custom Regex Variables)

Instead of serving static, hardcoded payloads, Mockery uses an embedded evaluation engine powered by Bogus. You can define template tokens directly inside your response schemas to output fresh, thread-safe test data on every single request:


  • {{faker.internet.email}} => Generates unique, dynamic emails.


  • {{path.id}} => Dynamically echoes path variables back in the payload.


  • {{request.body.name}} => Echoes incoming request properties for POST/PUT operations.


  • Custom Regex Variables: Need custom formatted data like specialized product SKUs, legacy account IDs, or specific code patterns? Mockery allows you to define your own Regex-based tokens directly in your configuration (e.g., {{regex:[A-Z]{3}-\d{4}-\b}}) to generate custom-patterned mock data on the fly without writing custom C# extensions.


2. Record & Replay Caching (Proxy Mode)


Need to test against external services like Stripe, PayPal, or internal microservices without incurring API fees or hitting rate limits?


In Proxy Mode, Mockery sits inline as a caching reverse proxy. On the first call, it forwards the request upstream to the real server, captures the response, and stores it in a zero-dependency, local LiteDB instance. On subsequent test runs, Mockery intercepts the call and serves the cached JSON instantly—making test suites running in GitHub Actions or GitLab CI ultra-fast and completely reliable offline!


3. Modular Static Fixtures & Flakiness Simulation

Serve static JSON, XML, or YAML fixtures directly from disk with advanced rule matching:


  • Multi-Response Rules: Serve different 200 OK payloads for the same endpoint depending on path parameters (/customer/get/{id}), query flags, or headers.


  • Fault Injection & Chaos: Toggle flakiness rules directly in your manifest to simulate network latency, rate limits, or intermittent 500/503 errors and backend database exceptions.


  • Stateful Write Overlay: Perform POST, PUT, or DELETE requests against static mocks using an ephemeral in-memory overlay—modifying state on the fly without ever altering your baseline fixture files on disk.


🛠️ Built for Modern DevOps & CI/CD Pipelines


Mockery was designed CLI-first to plug seamlessly into any automated delivery pipeline:


  • Zero Infrastructure Footprint: Embedded Kestrel web server and single-file LiteDB database mean zero external dependencies or database server setups required.


  • Monolith vs. Microservice Architecture: Run all endpoints under a single port or automatically spin up isolated web hosts per port to simulate multi-service architectures.


  • Docker & Pipeline Ready: Easily package Mockery into a sidecar container in GitHub Actions or GitLab CI to mock third-party backends during integration tests.

Tags:

← Previous Project
Project: Morpheus - AI based test automa...