+ Start a Discussion
Sushma  RamakrishnanSushma Ramakrishnan 

Need help in increasing Test Class Code Coverage

Hi All,

Need to increase the code coverage of the below Test Class to 75% from 70%.
Test Class :
@isTest
private class updateVisitorActionTest {
  
  
  
  static testMethod void testVisitorAction(){
    
    salyticsusa__Visitor_Action__c sal = new salyticsusa__Visitor_Action__c();
        sal.salyticsusa__Action_ID__c='5360F9BA5AFB4CF79D675C380D7A31FA1';
       sal.salyticsusa__URL__c='https://c.cs14.visual.force.com/apex/Supplierforce_Client_acc_page?id=001i0000006D2vL&sfdc.override=1';
  //      sal.salyticsusa__Visitor_Session__c='a1Si0000001IQsPEAW';
        salyticsusa__Visitor_Session__c ses= new salyticsusa__Visitor_Session__c();
        ses.salyticsusa__Non_Persistent_Session__c='123456756790';
        ses.salyticsusa__Persistent_Session__c='123456ADCVBHFDHKKNG5';
        salyticsusa__Visitor__c visitor= new salyticsusa__Visitor__c();
        visitor.salyticsusa__User_ID__c='005i0000000MgOkAAK';
        insert visitor;
        ses.salyticsusa__Visitor__c=visitor.Id;
        insert ses;
        sal.salyticsusa__Visitor_Session__c=ses.Id;
    insert sal;
    
     
    
  }

}
Trigger :
trigger updateVisitorAction on salyticsusa__Visitor_Action__c (before insert) {

  
 for(salyticsusa__Visitor_Action__c va : trigger.new) {
   string acctId;
   string contactId;
   string url = va.salyticsusa__URL__c;
   System.debug(url);
  if(url!=null){
   if(url.contains('Test_Ext_Cont_page?id=003')&&va.salyticsusa__Contact__c==null){
     System.debug('User viewing Contact with Custom Visual Force Page');
     contactId=url.substring(url.lastIndexOf('?id=')+4, url.lastIndexOf('?id=')+19);
     System.debug(contactId);
       va.salyticsusa__Contact__c= contactId;
           if(va.salyticsusa__Current_Action__c==null&&va.salyticsusa__Current_Object__c==null){
             System.debug('Visitor action and object');
          va.salyticsusa__Current_Action__c='View';
         va.salyticsusa__Current_Object__c='VF Contact Page';
           }
     
   }else if(url.contains('Test_Client_acc_page?id=001')&&va.salyticsusa__Account__c==null){
     System.debug('User viewing Account with Custom visual force Page');
     acctId=url.substring(url.lastIndexOf('?id=')+4, url.lastIndexOf('?id=')+19);
     System.debug(acctId);
     va.salyticsusa__Account__c= acctId;
        if(va.salyticsusa__Current_Action__c==null&&va.salyticsusa__Current_Object__c==null){
          System.debug('Visitor action and object');
        va.salyticsusa__Current_Action__c='View';
          va.salyticsusa__Current_Object__c='VF Account Page';
        }
     
   }

}

}

}
Thanks for any help in Advance...!


 
Best Answer chosen by Sushma Ramakrishnan
S TamanS Taman

Create another record for  salyticsusa__Visitor_Action__c in test class which satisfies this "if(url.contains('Testforce_Ext_Cont_page?id=003')&&va.salyticsusa__Contact__c==null)" 

something like   salyticsusa__Visitor_Action__c  sal2 = new salyticsusa__Visitor_Action__c();
        sal.salyticsusa__URL__c='https://c.cs14.visual.force.com/apex/Testforce_Ext_Cont_page?id= <Add value here>'

Also Most importantly as per Best practice test class should not include Hard coded values, though they are not impacting your trigger at the moment.  At line 10 of test class "001i0000006D2vL". It may hit back in future, insted create one more contact/account record in test class put its Id
 

All Answers

S TamanS Taman

Create another record for  salyticsusa__Visitor_Action__c in test class which satisfies this "if(url.contains('Testforce_Ext_Cont_page?id=003')&&va.salyticsusa__Contact__c==null)" 

something like   salyticsusa__Visitor_Action__c  sal2 = new salyticsusa__Visitor_Action__c();
        sal.salyticsusa__URL__c='https://c.cs14.visual.force.com/apex/Testforce_Ext_Cont_page?id= <Add value here>'

Also Most importantly as per Best practice test class should not include Hard coded values, though they are not impacting your trigger at the moment.  At line 10 of test class "001i0000006D2vL". It may hit back in future, insted create one more contact/account record in test class put its Id
 
This was selected as the best answer
karthikeyan perumalkarthikeyan perumal
Hello, 

Use below code, 
 
@isTest
private class updateVisitorActionTest {
  
  
  
  static testMethod void testVisitorAction(){
    
    salyticsusa__Visitor_Action__c sal = new salyticsusa__Visitor_Action__c();
        sal.salyticsusa__Action_ID__c='5360F9BA5AFB4CF79D675C380D7A31FA1';
        sal.salyticsusa__URL__c='https://c.cs14.visual.force.com/apex/Supplierforce_Client_acc_page?id=001i0000006D2vL&sfdc.override=1';
  //   sal.salyticsusa__Visitor_Session__c='a1Si0000001IQsPEAW';
        salyticsusa__Visitor_Session__c ses= new salyticsusa__Visitor_Session__c();
        ses.salyticsusa__Non_Persistent_Session__c='123456756790';
        ses.salyticsusa__Persistent_Session__c='123456ADCVBHFDHKKNG5';
        salyticsusa__Visitor__c visitor= new salyticsusa__Visitor__c();
        visitor.salyticsusa__User_ID__c='005i0000000MgOkAAK';
        insert visitor;
        ses.salyticsusa__Visitor__c=visitor.Id;
        insert ses;
        sal.salyticsusa__Visitor_Session__c=ses.Id;
        insert sal;
    
    
  }
  
  static testMethod void testVisitorAction()1{
   
        salyticsusa__Visitor_Action__c sal = new salyticsusa__Visitor_Action__c();
        sal.salyticsusa__Action_ID__c='5360F9BA5AFB4CF79D675C380D7A31FA1';
        sal.salyticsusa__URL__c='https://c.cs14.visual.force.com/apex/Supplierforce_Client_acc_page?id=003i0000006D2vL&sfdc.override=1';
  //   sal.salyticsusa__Visitor_Session__c='a1Si0000001IQsPEAW';
        salyticsusa__Visitor_Session__c ses= new salyticsusa__Visitor_Session__c();
        ses.salyticsusa__Non_Persistent_Session__c='123456756790';
        ses.salyticsusa__Persistent_Session__c='123456ADCVBHFDHKKNG5';
        salyticsusa__Visitor__c visitor= new salyticsusa__Visitor__c();
        visitor.salyticsusa__User_ID__c='005i0000000MgOkAAK';
        insert visitor;
        ses.salyticsusa__Visitor__c=visitor.Id;
        insert ses;
        sal.salyticsusa__Visitor_Session__c=ses.Id;
        insert sal;
    
    
  }

}
Hope this will help you. 

Thanks
karthik

 
Sushma  RamakrishnanSushma Ramakrishnan
Thanks so much for all responses...:)
@ S Taman : your solution worked for me...!
S TamanS Taman
Cheers!