分类目录

链接

2018 年 4 月
 1
2345678
9101112131415
16171819202122
23242526272829
30  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > Salesforce > 正文
salesforce rest api test class
Salesforce 暂无评论 阅读(563)

上一篇文章(salesforce rest api demo)我们写到salesforce如何写rest api,这篇我们将写如何写test class

  1. @isTest(SeeAllData=true)
  2. public class Api_user_profileTest
  3. {
  4.     static testMethod void Api_user_profileTest()
  5.     {
  6.         system.debug('start...');
  7.         Test.startTest();
  8.         String ReturnCode = '';
  9.         RestContext.request = new RestRequest();
  10.         RestContext.response = new RestResponse();
  11.         RestContext.request.requestURI = '/services/apexrest/user/profile/getPersonBasicInformation';  
  12.         RestContext.request.httpMethod = 'GET'; 
  13.  
  14.         //PersonType is null
  15.         RestContext.request.params.clear();
  16.         Api_user_profile.getPersonBasicInformation();     
  17.         ReturnCode =  getReturnCode(RestContext.response.responseBody.toString());
  18.         system.debug(ReturnCode);        
  19.         system.assert('99' == ReturnCode);  
  20.  
  21.         //PersonType is null        
  22.         RestContext.request.params.clear();
  23.         RestContext.request.addParameter('PersonID', '347081');
  24.         Api_user_profile.getPersonBasicInformation();     
  25.         ReturnCode =  getReturnCode(RestContext.response.responseBody.toString());
  26.         system.debug(ReturnCode);        
  27.         system.assert('99' == ReturnCode);  
  28.  
  29.         //PersonType is invalid
  30.         RestContext.request.params.clear();
  31.         RestContext.request.addParameter('PersonID', '347081');
  32.         RestContext.request.addParameter('PersonType', 'B');
  33.         Api_user_profile.getPersonBasicInformation();     
  34.         ReturnCode =  getReturnCode(RestContext.response.responseBody.toString());
  35.         system.debug(ReturnCode);        
  36.         system.assert('99' == ReturnCode);  
  37.  
  38.         //personid is not found
  39.         RestContext.request.params.clear();
  40.         RestContext.request.addParameter('PersonID', '1234567890');
  41.         RestContext.request.addParameter('PersonType', 'C');
  42.         Api_user_profile.getPersonBasicInformation();     
  43.         ReturnCode =  getReturnCode(RestContext.response.responseBody.toString());
  44.         system.debug(ReturnCode);        
  45.         system.assert('99' == ReturnCode);  
  46.         
  47.         
  48.         //all parameters is valid
  49.         RestContext.request.params.clear();
  50.         RestContext.request.addParameter('PersonID', '347081');
  51.         RestContext.request.addParameter('PersonType', 'C');
  52.         Api_user_profile.getPersonBasicInformation();     
  53.         ReturnCode =  getReturnCode(RestContext.response.responseBody.toString());
  54.         system.debug(ReturnCode);        
  55.         system.assert('00' == ReturnCode);  
  56.         
  57.         //another personid
  58.         RestContext.request.params.clear();
  59.         RestContext.request.addParameter('PersonID', '345792');
  60.         RestContext.request.addParameter('PersonType', 'A');
  61.         Api_user_profile.getPersonBasicInformation();     
  62.         ReturnCode =  getReturnCode(RestContext.response.responseBody.toString());
  63.         system.debug(ReturnCode);        
  64.         system.assert('00' == ReturnCode);  
  65.  
  66.         Test.stopTest(); 
  67.         system.debug('end');
  68.     }
  69.  
  70.  
  71.     static String getReturnCode(String text){
  72.         if(text == null || text.length()==0){
  73.             return 'error';
  74.         }
  75.         integer i = text.indexOf('ReturnCode'); 
  76.         String code = text.substring(i+13,i+15); 
  77.         return code;
  78.  
  79.     }
  80. }

测试一下:

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:salesforce rest api test class | Bruce's Blog

发表评论

留言无头像?