11 ms·
Ask HN: Best Way to Mock APIs in 2020?
I'm looking for a good way to Mock APIs for a sideproject, so far I haven't found any solution that has blown me away and I'm wondering If I'm missing anything.
I'm curious what people recommend for the best way to Mock APIs?
- zoontek 6y agoMight 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
- nmasse-itix 6y agoI was wondering if you had a chance to have a look at Microcks (https://microcks.io/ https://microcks.io/) ?
- deleted 6y ago[deleted]
- coold 6y agoI'm using Wiremock
- robjan 6y agoDepends 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.
- htyden 6y agohttps://intervene.dev/ https://intervene.dev/ has been very useful for me.
- lbroudoux 6y agoI 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 6y agoNice! Have you considered doing a free online SaaS version, rather than a self-hosted k8s version?
- lbroudoux 6y agoYes it’s something we have in mind for next months or so.
- mdaniel 6y ago> Keycloak 4.8.0 > MongoDB 3.4 That's a pretty heavyweight dependency list (and I'll leave my snide remarks about Mongo out of this) Does your usecase involve such high security for your mocks?
- jacobr 6y agoHaven’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.
- triztian 6y agoI'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 6y agoOh, 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/
- tiborsaas 6y agoI've used this: https://github.com/nock/nock https://github.com/nock/nock worked great for tests.
- baggsie 6y agoI'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.
- peheje 6y agoCan 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.
- emilsedgh 6y agoI personally am not a fan of too many abstractions. If the only reason for abstraction is testing, then mocking an interface seems like a more valid choice to me.
- xadoc 6y agoThe abstraction is there because in many static languages you will immediately have two implementations, the infrastructure one (database, API call, 3rd party) and the tests.
- jjpe 6y ago> I personally am not a fan of too many abstractions. Then you're likely in the wrong business, since most of what programming is is dealing with the various facets of one abstraction or another.
- TomMarius 6y agoThis is definitely a valid approach to software development, e.g. in embedded development (where you still might need to call APIs).
- emilsedgh 6y agoAbsolutely. I'm not saying I don't want to abstract anything or deal with any abstractions which can lead to ridiculous code [0]. I just prefer to abstract when I have more reasons that just testing. That can be done using mocking techniques. https://docs.spring.io/spring/docs/2.5.x/javadoc-api/org/springframework/aop/config/SimpleBeanFactoryAwareAspectInstanceFactory.html https://docs.spring.io/spring/docs/2.5.x/javadoc-api/org/spr...
- 6y ago
- xgenecloud 6y agoIf 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
- forresponse 6y agoI've used this on multiple projects: https://github.com/typicode/json-server https://github.com/typicode/json-server
- gerdusvz 6y agoyour mother was a SOAP request
- chrismorgan 6y agoI feel like that might have been better mockery for 2010 than 2020. While there’s something to be said for mockery that goes over someone’s head (so that you get to feel smugly superior, and they just feel vaguely discomfited because they can tell you were deriding them, but they’re not sure how), it can also be nice if they understand, so that they know just how insulted they should feel.
- mpweiher 6y agoAvoid mocking: https://blog.metaobject.com/2014/05/why-i-don-mock.html https://blog.metaobject.com/2014/05/why-i-don-mock.html
- XVincentX 6y agohttps://stoplight.io/open-source/prism https://stoplight.io/open-source/prism
- isuckatcoding 6y agoDon’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.
- vkku 6y agoMockito exists as an option
- colecut 6y agoI like this endorsement
- m0xte 6y agohttp://wiremock.org/ http://wiremock.org/ works for me
- mister_hn 6y agoApi-framework works really good for this
- davegb3 6y agoIf you like wiremock, try https://intervene.dev https://intervene.dev - config in TypeScript with automatic https and CORS. Open for questions + feedback [author]
- nerdywordy 6y agohttps://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.
- mcintyre1994 6y agoI’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.
- sidcool 6y agoI use Mountebank extensively and it's a pleasure to use.
- kahrensdd 6y agoThere 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.html https://docs.cypress.io/guides/guides/stubs-spies-and-clocks... 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/setting-up-mock/ https://learning.postman.com/docs/postman/mock-servers/setti... 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.
- thiht 6y agoShameless 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
- pinacarlos90 6y agoUse 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.
- jcpst 6y agoFor 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.
- lovetocode 6y agoA lot of Ruby’s test frameworks have this functionality built in. What technology are you using
- deleted 6y ago[deleted]
- poke111 6y agohttps://www.mock-server.com/ https://www.mock-server.com/
- hckr_news 6y agoNock and JSON server are useful libraries
- disposedtrolley 6y agoIf 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
- mdaniel 6y agoI 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
- tomakehurst 6y agoMockLab [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