function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
fiona gentryfiona gentry 

How to increase the code coverage from 76% to 90% with system.assert statements

Hi,

Wrote this test class for Integration apex class with heroku ,how to increase the test coverage form 76% to 90% and also write system.assert statements
 here is test apex class
@isTest
private class HerokuPANSyncTest {
    
    @isTest static void testCallout() {
        HerokuServices__c herokuServices =new HerokuServices__c();
        herokuServices.Name='BasicAccountDetails';
        herokuServices.EndPointUrl__c = 'https://account-tax-PAN-batch-sync-sta.herokuapp.com/basic_account_details';
        insert herokuServices;
        
        API_Proxy_Endpoints__c proxyEnd = new API_Proxy_Endpoints__c();
        proxyEnd.API_EndPoint__c = 'https://qat02.api.ABC.COM/oauth2/v6/tokens';
        proxyEnd.Name = 'OAUTH Endpoint';
        insert proxyEnd;
        
        API_Proxy_Endpoints__c proxyEndPAN = new API_Proxy_Endpoints__c();
        proxyEndPAN.API_EndPoint__c = 'https://PAN-sync-dev.herokuapp.com/api/updatePANInfo?env=QLAB02';
        proxyEndPAN.Name = 'PANUpdate';
        insert proxyEndPAN;
        
        HerokuServices__c herokuServices2 =new HerokuServices__c();
        herokuServices2.Name='PANUpdate';
        herokuServices2.EndPointUrl__c = 'https://b2c-PAN-sync.herokuapp.com/updatePANInfoConsumer';
       // herokuServices2.EndPointUrl__c = 'https://PAN-sync-dev-apigee.herokuapp.com/api/';
        insert herokuServices2;
        
        Apigee_Services__c apigService = new Apigee_Services__c();
        apigService.Name = 'APIGEE B2C Salesforce SaaS';
        apigService.Apigee_CS__c = 'SCRET';
        apigService.Apigee_CK__c = 'KEY';
        apigService.Apigee_Token_URL__c = 'https://qat03.api.ABC.COM/oauth2/v4/tokens';
        insert apigService;
        
        AuthToken__c token = new AuthToken__c();
        token.Name='heroku';
        token.Token_URL__c='https://pos-auth-app.herokuapp.com/services/oauth2/token';
        token.audience__c = 'https://account-tax-PAN-batch-sync-sta.herokuapp.com/';
        token.client_id__c = 'key';
        token.client_secret__c= 'secret';
        token.grant_type__c='client_credentials';
        insert token;
         
        // Set mock callout class 
        Test.seposck(HttpCallouposck.class, new MockHttpResponseGeneratorAPI());
        
        // Call method to test.
        // This causes a fake response to be sent
        // from the class that implements HttpCallouposck. 
        Test.starttest();
        HerokuPANSync basicAcct = new HerokuPANSync();
        basicAcct.getPANInfoUpdate('411057');
        Test.stoptest();
      }  
}



Here is apex class
public with sharing virtual class HerokuPANSync {
    @TestVisible
    public static final Integer MAX_TIMEOUT = 120000;
    
    public PANInfo getPANInfo(String PANNumber){
        
        //SM_Authentication auth = new SM_Authentication();
        //string access_token = auth.heroku_token();     
        String access_token = SM_APIHandler.generatetoken_SaaS_v6(); // Getting the Proxy token form APIGee
        
        //calling endpoint from custom settings
        //HerokuServices__c herokuServices = HerokuServices__c.getValues('PANUpdate');
        API_Proxy_Endpoints__c herokuServices = API_Proxy_Endpoints__c.getValues('PANUpdate');
        //string endPoint = herokuServices.EndPointUrl__c; 
        String endPoint = herokuServices.API_EndPoint__c;       
        HttpRequest req = new HttpRequest();
        req.setEndpoint(endPoint);
        req.setHeader('PANNumber', PANNumber);
        req.seSMethod('GET');
        req.setTimeout(MAX_TIMEOUT);        
        req.setHeader('Authorization', 'Bearer ' + access_token);
        
        // http call
        Http http = new Http(); 
        new HI_PopTokenGen().buildAuthKey(req);  //Building Auth key for Pop Token.       
        HTTPResponse res = http.send(req);        
        System.debug(res.getStatusCode()); 
        System.debug(res.getStatus());
        if(res.getStatusCode() == 200){            
            PANInfo response = (PANInfo)JSON.deserialize(res.getBody(), PANInfo.class);
            system.debug(res.getBody());
            System.debug('$$$$$ response'+response);            
            return response;            
        }        
        else{  
            System.debug('****Enter into Else *****');
            PANInfo response = new PANInfo();
           
            PAN__c PAN = new PAN__c();
            response.PAN = PAN;
            response.PAN.Last_Samson_Response_DateTime__c = system.now();
            
            response.PAN.Last_Samson_Status__c = '' + res.getStatusCode();  
            
            System.debug('response from samson'+ response);
            return response;    
        }        
       // return null;        
    }

    public void getPANInfoUpdate(String PANNumber){
        
        PANInfo PANResp = getPANInfo(PANNumber);
        if(PANResp != null){
            system.debug('PANResp id'+PANResp);
            PANResp.PAN.PAN_Number__c = PANNumber;
            PANResp.updateRecords();
        }      
    }
    
    public class PANInfo{
        public PAN__c PAN;

        public void updateRecords(){
            SavePoint sp = Database.setSavePoint();
            
            system.debug('PANInfo ss'+PAN);
            
            try{
                
                Database.upsert(PAN, PAN__c.PAN_Number__c, true);
            }
            catch(DMLException e){
                Database.rollback(sp);
                System.debug(e.getStackTraceString());
            }
        }
    }    
}




Your  help is highly appreciated
Fiona
SwethaSwetha (Salesforce Developers) 
HI Fiona,
Hope you are doing well!
I know this is an old thread.  Since this code is huge , does not highlight uncovered lines and requires an understanding of your implementation, it might not be possible to provide exact edit suggestions. However, the below articles give a good insight into how coverage can be improved
 
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines 
 
Examples of test classes:
https://salesforce.stackexchange.com/questions/63253/integration-salesforce-with-heroku
https://salesforce.stackexchange.com/questions/279400/mock-test-class-for-post-web-service-test-class
https://salesforce.stackexchange.com/questions/270759/need-help-in-writing-test-class-for-rest-api-class
 
If this information helps, please mark the answer as best. Thank you