- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-18-2022 06:55 AM
Hi Community,
My team is developing a service portal in Servicenow which includes several Outbound REST calls via an OAuth configuration. While writing ATFs, how can mocking be implemented ? I don't want the actual REST calls to happen while running ATFs, so can someone guide me on how to implement mocking in ATFs ?
Thanks!!
Solved! Go to Solution.
- Labels:
-
Automated Test Framework

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-20-2022 01:07 AM
Hi AJ
We can mock the APIs using Jasmine which is available in the step type 'Run Server Side Script'. Following is a quick example to get mock results for table API for user table:
(function(outputs, steps, params, stepResult, assertEqual) {
describe('mock example', function() {
it('should have mock api', function() {
var request = new sn_ws.RESTMessageV2();
request.setEndpoint(encodeURI('https://demoxyz.service-now.com/api/now/table/sys_user?sysparm_query=user_name=AZ')); //API to mock
//Following are optional
request.setBasicAuth(<userName>, <password>);
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader('Content-Type', 'application/json');
//Optional path end
request.setHttpMethod('GET');
spyOn(request, 'execute').and.returnValue('result:[{abc: "abc"}]'); //This is the actual mock response
var response = request.execute();
expect(response).toBe('result:[{abc: "abc"}]'));
});
});
})(outputs, steps, params, stepResult, assertEqual);
// uncomment the next line to execute this script as a jasmine test
jasmine.getEnv().execute();
--
Best Regards
Ankush
P.S. Please mark helpful/correct as appropriate to help the fellow community member in identifying the relevant answers.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-20-2022 01:07 AM
Hi AJ
We can mock the APIs using Jasmine which is available in the step type 'Run Server Side Script'. Following is a quick example to get mock results for table API for user table:
(function(outputs, steps, params, stepResult, assertEqual) {
describe('mock example', function() {
it('should have mock api', function() {
var request = new sn_ws.RESTMessageV2();
request.setEndpoint(encodeURI('https://demoxyz.service-now.com/api/now/table/sys_user?sysparm_query=user_name=AZ')); //API to mock
//Following are optional
request.setBasicAuth(<userName>, <password>);
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader('Content-Type', 'application/json');
//Optional path end
request.setHttpMethod('GET');
spyOn(request, 'execute').and.returnValue('result:[{abc: "abc"}]'); //This is the actual mock response
var response = request.execute();
expect(response).toBe('result:[{abc: "abc"}]'));
});
});
})(outputs, steps, params, stepResult, assertEqual);
// uncomment the next line to execute this script as a jasmine test
jasmine.getEnv().execute();
--
Best Regards
Ankush
P.S. Please mark helpful/correct as appropriate to help the fellow community member in identifying the relevant answers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-18-2022 02:09 PM
Hi Ankush,
We went through this but is still having doubts. Our Service Portal application is having a page, where there is a text box search. When a user types in a keyword, an Outbound REST API is called and a widget is loaded with the results from the REST endpoint. So, how could we add the mocking part in ATF Test Steps in this case?
If we add "Run Server Side Script" as a step, how could we pass the data from the Script to the widget that is loaded in UI ? and how do we stop the actual API call that would happen from the UI ? Sorry, but I'm not able to connect all these together.
Regards,
Ajay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-20-2023 02:02 AM
Precisely.
I had the same requirement, and worked out how to mock a ServiceNow REST message HTTP method for ATF tests.
I was not able to post the full solution here, so I posted it on stackoverflow: "How to mock an HTTP method of a REST message for ServiceNow ATF tests?".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2022 12:14 PM
Hi Ankush,
Thank you so much for the response!!!
We are making use of this and adding mocks in our service portal.