现在位置:
首页 > Salesforce > 正文
salesforce rest api test class
上一篇文章(salesforce rest api demo)我们写到salesforce如何写rest api,这篇我们将写如何写test class
- @isTest(SeeAllData=true)
- public class Api_user_profileTest
- {
- static testMethod void Api_user_profileTest()
- {
- system.debug('start...');
- Test.startTest();
- String ReturnCode = '';
- RestContext.request = new RestRequest();
- RestContext.response = new RestResponse();
- RestContext.request.requestURI = '/services/apexrest/user/profile/getPersonBasicInformation';
- RestContext.request.httpMethod = 'GET';
- //PersonType is null
- RestContext.request.params.clear();
- Api_user_profile.getPersonBasicInformation();
- ReturnCode = getReturnCode(RestContext.response.responseBody.toString());
- system.debug(ReturnCode);
- system.assert('99' == ReturnCode);
- //PersonType is null
- RestContext.request.params.clear();
- RestContext.request.addParameter('PersonID', '347081');
- Api_user_profile.getPersonBasicInformation();
- ReturnCode = getReturnCode(RestContext.response.responseBody.toString());
- system.debug(ReturnCode);
- system.assert('99' == ReturnCode);
- //PersonType is invalid
- RestContext.request.params.clear();
- RestContext.request.addParameter('PersonID', '347081');
- RestContext.request.addParameter('PersonType', 'B');
- Api_user_profile.getPersonBasicInformation();
- ReturnCode = getReturnCode(RestContext.response.responseBody.toString());
- system.debug(ReturnCode);
- system.assert('99' == ReturnCode);
- //personid is not found
- RestContext.request.params.clear();
- RestContext.request.addParameter('PersonID', '1234567890');
- RestContext.request.addParameter('PersonType', 'C');
- Api_user_profile.getPersonBasicInformation();
- ReturnCode = getReturnCode(RestContext.response.responseBody.toString());
- system.debug(ReturnCode);
- system.assert('99' == ReturnCode);
- //all parameters is valid
- RestContext.request.params.clear();
- RestContext.request.addParameter('PersonID', '347081');
- RestContext.request.addParameter('PersonType', 'C');
- Api_user_profile.getPersonBasicInformation();
- ReturnCode = getReturnCode(RestContext.response.responseBody.toString());
- system.debug(ReturnCode);
- system.assert('00' == ReturnCode);
- //another personid
- RestContext.request.params.clear();
- RestContext.request.addParameter('PersonID', '345792');
- RestContext.request.addParameter('PersonType', 'A');
- Api_user_profile.getPersonBasicInformation();
- ReturnCode = getReturnCode(RestContext.response.responseBody.toString());
- system.debug(ReturnCode);
- system.assert('00' == ReturnCode);
- Test.stopTest();
- system.debug('end');
- }
- static String getReturnCode(String text){
- if(text == null || text.length()==0){
- return 'error';
- }
- integer i = text.indexOf('ReturnCode');
- String code = text.substring(i+13,i+15);
- return code;
- }
- }
测试一下:
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · Salesforce VisualForce 使用apex:actionFunction调用后台方法
- · How to Call Salesforce API from Lightning Component
- · apex highlight userDefineLang for notepad++
- · 详解 Salesforce 15 和 18 位的ID
- · Salesforce Test class注意事项
- · salesforce Milestone clock is not running
- · salesforce apex traced the number of query
- · salesforce antscript无法拉取完整的profile
- · salesforce lightning report取URL参数
- · salesforce test class用现有数据测试
- · salesforce report get url parameters
- · salesforce display trigger error