0

I have A Given Sample Apex Code

 @HttpGet
    global static String doGet(){
        RestRequest req  = RestContext.request;
        RestResponse res = RestContext.response;
        try{
            String UserId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
            System.debug('UserId:'+UserId);
            if(UserId =='' || UserId ==null) UserId = userinfo.getUserId();
            return getProfileDetails(UserId);    
        }catch(Exception e){
            System.debug('Message1:'+e.getMessage());
            throw new CustomException(PG_Utils.getException(GENERIC_ERROR,GENERIC_ERROR_TYPE, CustomException__c.getValues('GEN0001').Error_Description__c));
        }
    }

The Above Code Represents In Post Man if you give portal User Id or Not Given any Id,It will Get the Particular Profile Details of User For that I have Wrote Test Class Like:

RestRequest req = new RestRequest(); 
req.requestURI='https://cs81.salesforce.com/services/apexrest/ProfileService/'+user1.id;
 req.httpMethod = 'GET';
 RestContext.request = req;
 BusinessProfile1.doGet();

I am unable to cover the Try Catch Block in Test Class,How to Cover Apex Class Try Catch Block in Test Class,Try Catch Block represents if any Wrong User Id is Given in postMan,it Should have Throw Error Message in PostMna,Please anayone Guide Me how to Cover Try Catch Block in Test Class

1

You have to make another call with an erroneous Id. Something like:

RestRequest okReq = new RestRequest(); 
okReq.requestURI='https://cs81.salesforce.com/services/apexrest/ProfileService/'+user1.id;
okReq.httpMethod = 'GET';
RestContext.request = okReq;
BusinessProfile1.doGet();

String wrongId = 'this is not an ID';

//since the method trows an exception, you need to try/catch it
try{
    RestRequest worngReq = new RestRequest(); 
    wrongReq.requestURI='https://cs81.salesforce.com/services/apexrest/ProfileService/'+wrongId;
    wronReq.httpMethod = 'GET';
    RestContext.request = wrongReq;
    BusinessProfile1.doGet();
}catch(Exception e){
    System.AssertEquals(e.getMessage(), PG_Utils.getException(GENERIC_ERROR,GENERIC_ERROR_TYPE, CustomException__c.getValues('GEN0001').Error_Description__c));
}
  • what is e.getmessage()? what does it contain? how it will be equal to PG_Utils.getException(GENERIC_ERROR,GENERIC_ERROR_TYPE, CustomException__c.getValues('GEN0001').Error_Description__c – user30475 Apr 28 '16 at 10:58
  • cananyone explain to me? – user30475 Apr 28 '16 at 10:58
  • When you catch an exception (in this case it's called "e") you can access lots of info. One of it is the message generated on the error. When you throw a new exception, you choose which message to display. In your case: throw new CustomException(--message--); – Fernando Gavinho Apr 28 '16 at 14:13
  • That simply asserts that the message you throw is the message you catch. =D – Fernando Gavinho Apr 28 '16 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy