Mocking an API Using SwaggerHub
Learn how to mock an API using SwaggerHub.
Mocking an API
Mocking an API is our chance to see a working version of the API up and running. SwaggerHub has a built-in API mocking service. It reads our OAS document and uses the definitions (and the example values) in that document to set up a mock API server that we can use to test against. As we’ve seen previously, the real value of an API prototype is that it allows us to test out the interface. Mocks are an excellent way to do that.
Adding the server
element to an OAS document
To get the SwaggerHub API mock up and running, we need to make sure we have a server
element in our OAS document that points to the SwaggerHub virtserver
. It is turned on when we create an OAS file, but we can also add it later by clicking the name of our loaded definition file in the top-left corner of our editor screen and then clicking the “Integrations” tab in the dropdown menu. When we do that, we’ll see “API Auto Mocking” as one of the options. To establish the mocking service with all the defaults, just press the dark arrow on the “API Auto Mocking” line. That initiates an instance of the mocking service for us based on the loaded API definition file. It also adds and updates the server’s entry in our OAS file, which looks something like this:
Get hands-on with 1200+ tech skills courses.