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
@anilbathula@@anilbathula@ 

test coverage from 59% to above

Hi guys,

 

can u plz help me to increase my test coverage from 59 to 100%

The code in red lines are not  covered in the test class

 

trigger code:-

=======================================================================================

 

trigger Updtcon on comments__c (before insert) {
 Set<Id> sobjectSetOfIds = new Set<Id>();

    Comments__c cms;
    opportunity opp;   
        
 
    for(Comments__c cs:trigger.New){
        cms=cs;    
        if(cms.Opportunity__c!=null && cms.Contact__c==null && cms.Finance__c==null){
            sobjectSetOfIds.add(cms.Opportunity__c);           
        }
    } 
    Map<Id,Opportunity>smap= new Map<Id, Opportunity>([Select id,Name,Borrower__c,(Select id,name from Finances__r)  from Opportunity where Id in : sobjectSetOfIds]);
                
    for(Comments__c s: trigger.new){
        if(smap.containsKey(s.Opportunity__c)){        
            s.Contact__c=smap.get(s.Opportunity__c).Borrower__c;  
            if(smap.get(s.Opportunity__c).finances__r.size()>0&&smap.get(s.Opportunity__c).finances__r!=null){            
            s.Finance__c=smap.get(s.Opportunity__c).finances__r[0].id; 
            }
            s.Comment_Created_from__c ='Opportunity  ::'+ '  '+smap.get(s.Opportunity__c).Name;
            s.written_by__c=UserInfo.getName();
            s.Written_date__c=system.Today();
        }
    }
    
    /* updating opportunity from contact object comments related list*/  

   for(Comments__c cs:trigger.New){
        cms=cs;    
        if(cms.Opportunity__c==null && cms.Contact__c!=null && cms.Finance__c==null){
           List<Opportunity> opportunities = [select Id, Name,Borrower__r.Name,(Select id,name,contact__c from Finances__r )from Opportunity where Borrower__r.Id  = :cms.Contact__c];         
        
           for(Opportunity oppn:opportunities){                        
               cs.Opportunity__c = oppn.id;
               if(oppn.finances__r.size()>0){           
               cs.Finance__c=oppn.finances__r[0].id;
               }
               cs.Comment_Created_from__c ='Contact ::'+'  '+ oppn.Borrower__r.Name;
               cs.written_by__c=UserInfo.getName();
               cs.Written_date__c=system.Today();
             
            }
        }
     }  
     
   /* updating opportunity and contact from finance */
   
   for(Comments__c cs:trigger.New){
        cms=cs; 
        if(cms.Finance__c!=null & cms.Contact__c==null&& cms.Opportunity__c==null){
            List<Finance__c> fc=[select id,Name,Opportunity__r.id,Opportunity__r.Borrower__r.id from Finance__c where id=:cms.Finance__c];
            for(finance__c f:fc){
                cs.Opportunity__c=f.opportunity__r.id;
                cs.Contact__c=f.Opportunity__r.Borrower__r.id;
                cs.Comment_Created_from__c ='Finance  ::'+'  '+ f.Name;
                cs.written_by__c=UserInfo.getName();
                cs.Written_date__c=system.Today();
            }
        }
   }

=======================================================================================

test class:-

=======================================================================================

@isTest
private class Updtcon {
static testMethod void Updtcon () {

Contact C= new contact(LastName='Anil');
insert c;

list<opportunity> o=new list <opportunity>();
for(integer i=0;i<200;i++){
Opportunity oppn=new opportunity(name='Anil',StageName='prospecting',CloseDate=system.today(),Borrower__c=c.id);
o.add(oppn);
}
insert o;

// System.assertEquals(1,o.size());

Finance__c f=new finance__c(Name='payment',opportunity__c=o[0].id,Contact__c=o[0].Borrower__r.id);
insert f;

comments__c cms=new comments__c(Name='Anil',opportunity__c=o[0].id,Contact__c=o[0].Borrower__r.id);
insert cms;

}
}

============================================================================================

 

Thanks

Anil.B

 

Best Answer chosen by Admin (Salesforce Developers) 
@anilbathula@@anilbathula@

Hi guys,

 

Thanks for all ur responses

I got the 100% test coverage for the trigger:-

=====================================

@isTest 
private class Updtcon {
    static testMethod void Updtcon () {      
    
    Contact C= new contact(LastName='Anil');
    insert c;    
 
    Opportunity o=new opportunity(name='Anil',StageName='prospecting',CloseDate=system.today(),Borrower__c=c.id);
     insert o;
         
    Finance__c f=new finance__c(Name='payment',opportunity__c=o.id,Contact__c=o.Borrower__r.id);
    insert f;   
   
    Comments__c cm=new comments__c(Name='Raghu',Contact__c=c.id);
    insert cm;
   
    comments__c cms=new comments__c(Name='Anil',opportunity__c=o.id);
    insert cms; 
    
    Comments__c cs=new comments__c(Name='Anil',Finance__c=f.id);
    insert cs;       
    
    }
}

 

 

Thanks

Anil.B

 

All Answers

SamReadySamReady

Hi Anil,

 

If you click the "Run Test" button from your test class you should see your trigger somewhere in the bottom list under Code Coverage. When you click on the number next to it (the percentage of whats covered) it will highlight in red what lines you are not hitting with your test coverage.

 

I would start by trying to go through that yourself, and if you still cannot reach 75% minimum, paste in the [red] portions of your trigger that are not being hit so it is easier for others to help you figure out whats missing. 

 

Cheers,

Samantha

@anilbathula@@anilbathula@

HI samantha,

 

Thanks for remembering me the mistake.

the above red lines  code is not getting coverage .

 

Thanks

Anil.B

 

SamuelDeRyckeSamuelDeRycke

Both uncovered blocks of code are conditionally executed 'if .. then do it '.

 

  if(cms.Opportunity__c==null && cms.Contact__c!=null && cms.Finance__c==null){

 so if you want to have test coverage ...  make sure your test data passes your condition.   If you also have 'else then' code, have another test method with different data, or do updates to your data while running the same code several times.

 

 

@anilbathula@@anilbathula@

Hi guys,

 

Thanks for all ur responses

I got the 100% test coverage for the trigger:-

=====================================

@isTest 
private class Updtcon {
    static testMethod void Updtcon () {      
    
    Contact C= new contact(LastName='Anil');
    insert c;    
 
    Opportunity o=new opportunity(name='Anil',StageName='prospecting',CloseDate=system.today(),Borrower__c=c.id);
     insert o;
         
    Finance__c f=new finance__c(Name='payment',opportunity__c=o.id,Contact__c=o.Borrower__r.id);
    insert f;   
   
    Comments__c cm=new comments__c(Name='Raghu',Contact__c=c.id);
    insert cm;
   
    comments__c cms=new comments__c(Name='Anil',opportunity__c=o.id);
    insert cms; 
    
    Comments__c cs=new comments__c(Name='Anil',Finance__c=f.id);
    insert cs;       
    
    }
}

 

 

Thanks

Anil.B

 

This was selected as the best answer