You need to sign in to do that
Don't have an account?
Maria22
Unable to cover Application Exception in catch block test class
HI All,
I got stuck at one place where I am not able to cover a particular catch block consisting application exception. Currently my test class is getting pass succefully with 72% coverage but not able to cover one catch block.
Below is my class:
Below is my test class:
Below is the catch block which I am not able to cover:
Any help will be graeatly appreciated.
Many thanks in advance
I got stuck at one place where I am not able to cover a particular catch block consisting application exception. Currently my test class is getting pass succefully with 72% coverage but not able to cover one catch block.
Below is my class:
@RestResource(urlMapping='/v1/someurl/*') global with sharing class VerifiableAPI { private static BasicRestResponse response; @HttpPost global static void doPost(Request request) { Exception ex = null; Savepoint sp = Database.setSavepoint(); CareService svr = null; try { svr =CareService(request, CareService.APIType.VERIFIABLE); svr.doPost(); response = new RestResponseSuccess(); } catch (ApplicationException e) { if (response == null) { response = new RestResponseError(); } response.setError('INVALID_REQUEST', e.getMessage()); ex = e; Database.rollback(sp); } catch (Exception e) { if (response == null) { response = new RestResponseError(); } response.setError('INTERNAL_ERROR', e.getMessage()); ex = e; Database.rollback(sp); } if (ex != null) { ApplicationLog.LogMessage('Error', 'CareVerifiableAPI', 'doPost', null, null, JSON.serialize(response), JSON.serialize(RestContext.request), ex); } response.publish(); } /** * Request of the API Call */ global class Request { global String consent; } }
Below is my test class:
@isTest private class VerifiableAPITest { static testMethod void myUnitTest() { ; VerifiableAPI.Request reqst=new VerifiableAPI.Request(); reqst.consent='consent'; String JsonMsg=JSON.serialize(reqst); Test.startTest(); RestRequest req = new RestRequest(); RestResponse res = new RestResponse(); req.requestURI = '/services/apexrest/v1/demo'; //Request URL req.httpMethod = 'POST';//HTTP Request Type req.requestBody = Blob.valueof(JsonMsg); RestContext.request = req; RestContext.response= res; BasicRestResponse resp= new BasicRestResponse(); resp.setError('title', 'detail'); resp.setData(null); resp.getData(); resp.publish(); VerifiableAPI.doPost(reqst); Test.stopTest(); } }
Below is the catch block which I am not able to cover:
response = new RestResponseSuccess(); } catch (ApplicationException e) { if (response == null) { response = new RestResponseError(); } response.setError('INVALID_REQUEST', e.getMessage()); ex = e; Database.rollback(sp); }
Any help will be graeatly appreciated.
Many thanks in advance
Already you have one catch block, you can remove below one. i think it is not required.
catch (ApplicationException e) { if (response == null) { response = new RestResponseError(); } response.setError('INVALID_REQUEST', e.getMessage()); ex = e; Database.rollback(sp);
Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.
Thanks
Varaprasad
@For Salesforce Project Support: varaprasad4sfdc@gmail.com
Salesforce latest interview questions :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1
I dont whether the below works for you as you expected.But this iwll be one of the alternative :
Modify your class as:
Thanks
Hope this will be helpful.
I appreciateyour response.Application exception catch block is there for some intention and I can't remove in anyway just for sack of code coverage.
Kindly help me to cover catch block or guide me in some direction.One thing is sure I can't remove catch block as we are following some standard best practices.
Any help will be greatly appreciated.
Many thanks in advance