
Ask HN: Best Way to Mock APIs in 2020? - HiddenCanary
I&#x27;m looking for a good way to Mock APIs for a sideproject, so far I haven&#x27;t found any solution that has blown me away and I&#x27;m wondering If I&#x27;m missing anything.<p>I&#x27;m curious what people recommend for the best way to Mock APIs?
======
triztian
I've found mitmproxy ([https://mitmproxy.org](https://mitmproxy.org)) in
reverse mode really useful. I'm a mobile dev and when the backend work is
still pending I usually mock the backend requirements by selectively patching
or adding endpoint support, it can be used like so:

    
    
        mitmproxy --mode reverse:https://my.privateapi.com -s ./path/to/mockscript.py
    
    

This allows me to implement most of the UI and server interactions w/o being
blocked by server work.

~~~
e12e
Oh, this looks interesting. Thank you for the pointer.

I think we can use both: [https://docs.mitmproxy.org/stable/addons-
scripting/](https://docs.mitmproxy.org/stable/addons-scripting/)

For mocking, and dump/replay for debugging for some of our projects.

[https://docs.mitmproxy.org/stable/tute-
clientreplay/](https://docs.mitmproxy.org/stable/tute-clientreplay/)

------
peheje
Can someone explain to me what this is all about? Shouldn't you hide all your
API calls behind an interface, and then mock that interface? Dependency
inversion principle, depend upon abstractions and all that.

~~~
pydry
I've gone through this process a few times:

1) Join a project and fume about how they didn't write nice, clean, "testable"
code.

2) Build some sort of integration testing framework that also mocks out REST
API calls relatively cheaply (e.g. using recording/playback against a live
system).

3) Realize that I can now refactor the code such that it is nice, clean and
more easily "unit testable".

4) Realize that with an effective and easy to use, fast integration testing
framework the lack of dependency inversion doesn't bother me _nearly_ as much
as it used to.

5) Wonder if dependency inversion is actually more of a hack to deal with the
sheer utter crappiness of unit tests... and whether making vast architectural
changes to code in order to accommodate the sheer ineffectiveness of the
current state of test tooling is _really_ the height of wisdom after all.

In a number of these projects there were noises about rewriting the whole damn
thing in a different language (usually because management wanted to
consolidate hiring around fewer languages), and it occurred to me that it
would actually be kind of great to write tests where the entire implementation
_including the language itself_ could be swapped out without really changing
the tests.

~~~
moreaccountspls
Totally agree with you. I think the industry is going through an "emperor has
no clothes" thing with unit tests and dependency inversion at the moment.

The creator of BDD gave this talk a few years ago, which resonated with me:
[https://www.youtube.com/watch?v=4Y0tOi7QWqM](https://www.youtube.com/watch?v=4Y0tOi7QWqM)

> it occurred to me that it would actually be kind of great to write tests
> where the entire implementation including the language itself could be
> swapped out without really changing the tests.

This is exactly what I've been doing and advocating for for a while. Don't
mock things like databases at all in your code. If you want to use a mock, use
a "first class" mock like
[https://cloud.google.com/bigtable/docs/emulator](https://cloud.google.com/bigtable/docs/emulator).

The other thing is that TDD was born in a world where containers aren't the
norm. All the pain of higher level testing really comes up from setting up an
appropriate environment. If you're already using Docker or k8s, that pain is
pretty much 0.

------
robjan
Depends how advanced the mocking behaviour requirements are. For most
projects, I find json-server sufficient.

Remember that perfect is the enemy of good enough. If you're looking to be
blown away by your mock server you may never get round to building your
product.

------
nerdywordy
[https://miragejs.com/](https://miragejs.com/) is a great complement to
frontend first development. Some folks on my team use it and love it. I tend
to dev from back to front so I don't run into the need as much. If I do I
typically just mock a JSON response in my frontend call function. Depending on
the need I'll sometimes set the call to return an error response as well.

------
jcpst
For a side project? I think that makes a difference here.

What I’ve been doing lately, in my own time outside of work, is setting up a
postgres DB, pointing postgraphile to it, then I have the API out of the way.

From there, if I’m consuming that API in a statically-typed language, I’ll
look at generating the models I need from the graphql schema with quicktype.

That process is usually quick enough that there’s no reason to mock.

I suppose if I was doing UI first, I would just have the repository layer
that’s abstracting my calls to whatever just have a function that returns a
dumb static object in the shape I expect.

------
zoontek
Might not be the best, but the ones I use:

For REST API: [https://mockoon.com/](https://mockoon.com/)

For GraphQL: [https://github.com/APIs-guru/graphql-
faker](https://github.com/APIs-guru/graphql-faker)

------
kahrensdd
There are lots of variations depending upon your use case.

For unit testing and CI you may want mock objects that are implemented in the
same language as your code. Google search for "mock object <language>". That's
where you'll find Mockito (Java) or Mocha Spy (NodeJS) or Testify (golang).
This list never ends.

Specifically for unit testing of a UI, you may want your browser driver to
handle this, ex: Cypress has built-in support for mock AJAX endpoints.
[https://docs.cypress.io/guides/guides/stubs-spies-and-
clocks...](https://docs.cypress.io/guides/guides/stubs-spies-and-clocks.html)

If you want an endpoint you can call, Postman has a feature for this, there
are several others like this in the comments (JSON Server, mmock, mountebank,
etc.). [https://learning.postman.com/docs/postman/mock-
servers/setti...](https://learning.postman.com/docs/postman/mock-
servers/setting-up-mock/)

If you need to capture traffic, check out goreplay or mitmproxy:
[https://github.com/buger/goreplay](https://github.com/buger/goreplay)
[https://docs.mitmproxy.org/stable/](https://docs.mitmproxy.org/stable/)

There is a whole class of "VCR" projects for recording traffic, these tend to
be language specific (VCR is in Ruby), but there are ports to other languages:
[https://github.com/vcr/vcr](https://github.com/vcr/vcr)
[https://github.com/bblimke/webmock](https://github.com/bblimke/webmock)

The vendor products tend to be labelled Service Virtualization. I used to work
for one of those companies, ITKO, we were acquired by CA Technologies (now
Broadcom) in 2011. There are vendor products from Micro Focus, Tricentis,
Broadcom, Parasoft, etc.

It's important to think about your use case: local development, unit testing,
CI, integration testing, performance testing, recording vs. programming,
protocol support, payload support, etc. Many of the tools focus on just a
subset of these areas.

------
forresponse
I've used this on multiple projects: [https://github.com/typicode/json-
server](https://github.com/typicode/json-server)

------
tiborsaas
I've used this: [https://github.com/nock/nock](https://github.com/nock/nock)
worked great for tests.

------
XVincentX
[https://stoplight.io/open-source/prism](https://stoplight.io/open-
source/prism)

------
baggsie
I've found Hasura to be great for this, maybe slightly more longer to set up
but you get a full CRUD graphql setup for little effort.

------
mpweiher
Avoid mocking:

[https://blog.metaobject.com/2014/05/why-i-don-
mock.html](https://blog.metaobject.com/2014/05/why-i-don-mock.html)

------
m0xte
[http://wiremock.org/](http://wiremock.org/) works for me

------
mdaniel
I readily admit I haven't evaluated every one of the comprehensive URLs in
this thread, but back when I was doing integration work between my project and
both GitHub and Jira, I found the scripting support in SoauUI
([https://github.com/smartbear/soapui/tree/release-5.5.0](https://github.com/smartbear/soapui/tree/release-5.5.0))
allowed me to model some dynamic behavior, and use the fallback responses when
I didn't care about specifying every single one of them

Despite its name, SoapUI has strong support for REST, and with a little
massaging maybe even GraphQL also

------
xgenecloud
If you have a database - try this
[https://github.com/xgenecloud/xgenecloud](https://github.com/xgenecloud/xgenecloud)

Automatically generates REST & GraphQL APIs within seconds from your database
schema.

Here is a demo on how to create 8000+ REST APIs within 10 seconds -
[https://youtu.be/NtCwnlLudnk](https://youtu.be/NtCwnlLudnk)

Not just mock - you may even consider writing the full backend on it.

Im the creator - happy to answer any questions.

edit : added demo link

------
disposedtrolley
If the API provider supplies an OpenAPI definition, you can give API Sprout a
try. It’ll parse the definition and spin up a mock server returning the
example data you want. You can also modify the OpenAPI definition manually if
you need different data to be returned.

[https://github.com/danielgtaylor/apisprout](https://github.com/danielgtaylor/apisprout)

------
jacobr
Haven’t tried it yet, but
[https://www.npmjs.com/package/msw](https://www.npmjs.com/package/msw) looks
very interesting. It uses Service Workers in the browser to intercept requests
to the APIs, instead of running a real mock server.

There’s support for mocking in Node as well for SSR and jsdom tests.

------
pinacarlos90
Use swagger-openAPI3 and generate a spec. You can then use the spec to spin up
clients or import to a API management tool and produce mock data, with support
for authentication and more. I recommend using swashbuckle (for .netCore) if
you already have the code for your API, it allows you to quickly produce spec
from code.

------
coold
I'm using Wiremock

------
thiht
Shameless plug: I developed Smocker[1] for exactly this use case. It's not the
only solution but we have a few features not present anywhere else that
matched exactly our use case.

Hope it can help you :)

[1]: [https://smocker.dev](https://smocker.dev)

------
mcintyre1994
I’ve really liked working with mmock
[https://github.com/jmartin82/mmock](https://github.com/jmartin82/mmock)

It has a nice API for inspecting the requests made to the mock server and a
nice web UI for debugging too.

------
lbroudoux
I would suggest having a look at Microcks
([https://microcks.io](https://microcks.io)) as a way to produce mocks from
OpenAPI contracts, Postman collection or other assets...

Full disclosure : I am the founder of the project ;-)

~~~
HiddenCanary
Nice! Have you considered doing a free online SaaS version, rather than a
self-hosted k8s version?

~~~
lbroudoux
Yes it’s something we have in mind for next months or so.

------
tomakehurst
MockLab [1] is a SaaS mocking tool built on top of WireMock.

Worth a look if you use Swagger/OpenAPI as it'll accept this as an import
format.

[1]: [http://mocklab.io](http://mocklab.io)

------
davegb3
If you like wiremock, try [https://intervene.dev](https://intervene.dev) \-
config in TypeScript with automatic https and CORS.

Open for questions + feedback [author]

------
isuckatcoding
Don’t bother with mock APIs tbh. I assume you’re trying to unit test the front
end?

If anytime, write integration tests using something like selenium or whatever
the latest hotness is.

------
sidcool
I use Mountebank extensively and it's a pleasure to use.

------
htyden
[https://intervene.dev/](https://intervene.dev/) has been very useful for me.

------
poke111
[https://www.mock-server.com/](https://www.mock-server.com/)

------
hckr_news
Nock and JSON server are useful libraries

------
lovetocode
A lot of Ruby’s test frameworks have this functionality built in. What
technology are you using

------
mister_hn
Api-framework works really good for this

------
vkku
Mockito exists as an option

~~~
colecut
I like this endorsement

------
nmasse-itix
I was wondering if you had a chance to have a look at Microcks
([https://microcks.io/](https://microcks.io/)) ?

