Capture Mock Data
  • 11 Jan 2023
  • Dark
    Light
  • PDF

Capture Mock Data

  • Dark
    Light
  • PDF

Article summary

Capture Mock Data from Postman

This image illustrates the response body when mock data is captured using Postman.

Capture Mock Data from Postman

Capture Mock Data from Test

  • When the isSaveMockData flag is set to true (default is false) and the stub flag is set to false (default is true) in the adapterTestIntegration.js file, and you run a true integration test, it will make all API calls to the other system and save the responses into mock data files. It will also edit the appropriate action.json file so that the next time a call is made in standalone mode, it will return the new mock data.
  • The saveMockData method is already in the individual calls built by the Adapter Builder. This method does not do anything unless the two flags are set properly.

Example: Adaptertestintegration.js

const isSaveMockData = true;
const stub = false;
...

describe ('#createDeployment-errors', () => {
  it('should work if integrated or standalone with mockdata', (done) => {
    try {
      a.createDeployment(apisApiId, apisCreateDeploymentBodyParam, null, 
(data, error) => {
        try {
          runCommonAsserts(data, error);
          if (stub) {
            assert.equal('string', data.response.CreatedDate);
            assert.equal('string', data.response.DeploymentId);
            ...
          } else {
            assert.equal('string', data.response.CreatedDate);
          }
          saveMockData('Apis', 'createDeployment', 'default', data);
          done();
        } catch (err) {
          log.error(`Test Failure: ${err}`);
          done(err);
        }
      });
    } catch (error) {
      log.error(`Adapter Exception: ${error}`);
      done(error);
    }
  }).timeout(attemptTimeout);
});
  • There is another property that is also in the request section – return_raw. This property makes the raw response from the other system available in the result that was returned. The saveMockData method will use this data if it is available since it has not been translated. In contrast, if you are using translation, saveMockData will save the translated response, and you will then need to edit the data and revert the translation.

Example: Adaptertestintegration.js


return_raw: true,
...

describe ('#createDeployment-errors', () => {
  it('should work if integrated or standalone with mockdata', (done) => {
    try {
      a.createDeployment(apisApiId, apisCreateDeploymentBodyParam, null, 
(data, error) => {
        try {
          runCommonAsserts(data, error);
          if (stub) {
            assert.equal('string', data.response.CreatedDate);
            assert.equal('string', data.response.DeploymentId);
            ...
          } else {
            assert.equal('string', data.response.CreatedDate);
          }
          saveMockData('Apis', 'createDeployment', 'default', data);
          done();
        } catch (err) {
          log.error(`Test Failure: ${err}`);
          done(err);
        }
      });
    } catch (error) {
    log.error(`Adapter Exception: ${error}`);
      done(error);
    }
  }).timeout(attemptTimeout);
});

Capture Mock Data from Adapter Log

You need to have the adapter in debug mode in order to capture these logs. Then you can use these logs for mock data for future testing by storing the response part of the log into the mock data file for the specific call.

CALL RETURN – This is the full response prior to any JSON parsing.

ADAPTER LOGS

debug: Test-eai-connectorRest-makeRequest: CALL RETURN:  
{"status":"success","code":201,"response":"{\n\"type\" : 
\"oci/card\",\n\"id\" : 308,\n\"href\" : 
\"oci/card/308\",\n\"transientAttributes\"  : { },\n\"key\" :
{\n\"type\" : \"oci/cardKey\",\n\"keyValue\"  : 308\n}\n}","redirects":0,"tripTime":"197ms"}

CALL RESPONSE –This is the response or a partial response after JSON parsing.

ADAPTER LOGS

debug: Test-eai-restHandler-handleRestRequest: RESPONSE: 
{"type":"oci/card","id":308,"href":"oci/card/308","transientAttributes"
:{},"key" :{"type":"oci/cardKey","keyValue":308}}

Was this article helpful?

Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.