How to Use Mock API For Testing With BlazeMeter
February 9, 2021

How to Use Mock API For Testing With BlazeMeter

API Testing

​​​How do you use a mock API for testing? Find out in this blog!

Back to top

What Is Mock API Testing?

Mock API testing is the process of having a mock server API imitate a real API server by providing a realistic response. 

If you’ve already used the Mock Services feature of BlazeMeter to create a mock API for testing, you know how useful it can be when you are running tests but certain services are not available. For example, when you are using third party services. In this blog post, you will learn how to create a Mock Service, from the response to a BlazeMeter API Monitoring (Runscope) test. 

The first section of this article will get you up and running quickly, providing easy steps for importing a Runscope test that will create a mock service. The second section will go into a little more detail, providing an overview of what each step from the first section does.

Back to top

Overview: Quick Creation of a BlazeMeter Mock API For Testing

To get started, you can download this file and import it as a new test in the API Monitoring tab of BlazeMeter. (You can read more about how to import a test here.)

You will be using both the BlazeMeter and Runscope APIs for this test. You can read about how to create a BlazeMeter API token here and a Runscope API token here.

Once you’ve imported this test, you will need to configure your environment with some initial variables:

  • harborId — 5c544422c7dc9735767b23ce //specifies the location to run the mock service from -- this is for the public US East (Virginia) location
  • shipId — 5d3ccab3526ad28f53205574 //specifies the location to run the mock service from -- this is for the public US East (Virginia) location
  • count — {{timestamp}} // a unique value so that the transaction is uniquely identified
  • mockName — a unique name for the Mock Service
  • runscopeBucketKey — a Runscope bucket key for testing with the request you want to mock
  • runscopeTestId — a Runscope test id for testing with the request you want to mock
  • runscopeTestRunId — Runscope test run id for test with request you want to mock
  • runscopeStepToMock — the test step number of the request you want to mock (note: the step count should include skipped steps)
  • runscopeToken — the Runscope API Token (see Step 1 here on creating a token)
  • workspaceId — your BlazeMeter workspace ID in which you want to create your mock service (see here on how to find your workspace ID) 
mock service

You will also need to configure the Basic Authentication on the Authentication tab of the environment. You will use your BlazeMeter API credentials here.

Enter blazemeter credentials

Once you’ve defined these initial variables and the Authentication tab, you are all set to go.  

Simply click Run Now at the top left of your screen, and you will kick off a Runscope test that will retrieve the test you specified and create a Mock Service.

 

Example of results

You’ll notice the last step of this test makes a request to the running mock API for testing. You can now use this URL in other tests -- be it performance testing, API Monitoring or outside BlazeMeter altogether, to mock the response given by the original.

Request for running a mock
Back to top

How to Create Mock APIs for Testing: Step-by-Step

So now let’s explore each step of this test in a little more detail .

Step 1: Get the Test Result from Runscope

mock service

The first step in this test retrieves the Runscope test you’ve specified in your environment. It uses the Test Result Detail endpoint and extracts the UUID for the step that has the request you want to mock. Note that we cannot create this variable from the Variables tab. This is because you cannot reference a variable on that tab. We need to reference it since we are trying to specify which {{step}} of the test we want to take. So instead, the variable is created in a post-response script. (If you wanted to manually specify the step, you would be able to use the variables tab.)

Also note that this step (and the next step) use the Runscope API, so therefore there is a pre-request script that replaces the environment configured Authorization header (for BlazeMeter API) with a Runscope API token.

Step 2: Get the Body from Runscope

mock service

The second step uses the Test Result Step Detail endpoint to take the headers and body of the response you want to mock and assign them to variables to use later in the test. Note again that this needs to be done using a post response script. In the case of the body, this is because it needs to be base64 encoded when we upload it to the Mock Services API; for the headers, it is because they need to be reformatted for the Mock Services API.

Step 3: Create a Service

mock service

The third step uses the Create a Service endpoint to create the service that will contain the Mock Service in your BlazeMeter account. You will pass the Service ID that comes back from this in subsequent steps, so it is assigned to a variable here.

A Service is identified by its name. If the name you use for the service already exists, this step will return the Service ID for the service with that name (and you will get a 200 response). If the name is new, then a new service is created, and you will get a 201 response. Note the assertions tab that this step has been accounted for.

Step 4: Add a Mock Transaction

mock service

In order to create the Mock Service, you will first need a transaction to assign to that Mock Service. This is done with the fourth step, using the Create Transaction endpoint. The headers and body that were retrieved in step 2 are passed to this step. You can see in this request that the body (the content property) is base64 encoded. The transactionId that is produced is stored to a variable to use when creating the Mock Service.

Step 5: Create a Mock Service

mock service

Now that you have a transaction, you can create a Mock Service using the Create Mock Services endpoint. As configured, this mock service will run from the US Virginia public location, but you can customize this with a different shipId/harborId. You can also customize the thinkTime if you want it to take longer for the mock service to respond.

This step will give you back a mockServiceId that is assigned to a variable for subsequent use.

Step 6: Run Your Mock Service

mock service

Once the Mock Service is created, in order to use it you need to call the Deploy Mock Services endpoint to start running the mock service and make it usable.

Step 7: Pause the Test

mock service

It takes some time for the mock service to actually spin up, so by inserting a pause step here you give it time to work. If you find that you are getting a 503 response in step 9, increase the wait time here.

Step 8: Get the Mock Service

mock service

A request to the Get Mock Service endpoint will give you the URL for the (now running) Mock Service endpoint. This is the URL you can use anywhere else to get your Mock response.

Step 9: Get the Mock and Compare

mock service

This step is just to illustrate that the Mock Service is, in fact, up and running and returning the data expected.

Back to top

Conclusion

Congratulations — if you’ve followed the steps here you’ve successfully created a Mock Service from a Runscope test result. You can make additional modifications to this Mock Service either via API or from the Mock Services tab within BlazeMeter. Happy Mocking!

START TESTING NOW

Back to top