You need to sign in to do that
Don't have an account?
Maria22
How to cover application exception in 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
Thanks for your response. I tried the code which you suggested still code coverage is 72% as earlier and same lines of code are uncovered.
The code which I posted was also covering the second catch block(exception e) and the one which you suggetsed also covering the same catch block(exception e).Neither your code nor my code is covering first catch block(ApplicationException e).
Complete class is getting covered except below lines of code:
which also results not covering more than 75% and current code coverage stands at 72%.
Any help will be greatly appreciated.
Many thanks in advance