+ Start a Discussion
krishnagkrishnag 

test coverage problem

hi i have written a test method for a contoller class.when i run the test i am getting 100% class coverage. The problem is i am updating a custom object using this controller. I have created a trigger so that whenewver a update is done on the custom object filed.if the email in custom obect existing in lead or contact it should update those objects correspondingly. so the trigger coverage in the controller class is only 52% since the email is existing in only lead or contact.

 

the overall coverage came down to 65%. How can i avoid this and deploy the controller.

 

 

global with sharing class preferenc 
{

    public PageReference cancel() {
        PageReference p = Page.user_preference;
         p.setRedirect(true);
          return p;
    }


    

 public string guestid;
    public guestuser__c gus{get;set;}

    public preferenc() 
    {
    this.guestid=ApexPages.currentPage().getParameters().get('paramID');   
        if(guestid!=null||guestid!='')
        {
            getdetails(guestid); 
        }   

    }

    
    
    public void getdetails(string guestid)
    {
        List<guestuser__c> guests=[select id,Email__c,Phone__c,Title__c,Email_Optout__c,A_H_interests__c,After_Markets_interests__c,Construction_interests__c,D_O_interests__c,Energy_interests__c,Environmental_interests__c,Financial_Enterprises_interests__c,Healthcare_interests__c,Manufacturing_interests__c,Property_interests__c,RealEstate_interests__c,Technology_interests__c,Windstorm_notification_interests__c from guestuser__c where id=:guestid];
        if(guests.size()>0)
        {
            gus=guests[0];
        }
        else
        {
            
        }
    }
    public PageReference save()
    {
        
          update gus;
          PageReference t = Page.thanks;
          t.setRedirect(true);
          return t;  
    } 
    
    static testMethod void testpreferenc()
    {
     
       ApexPages.currentPage().getParameters().put('paramID','a0RQ0000000B0On');
       preferenc controller = new preferenc();
       controller.save();    
       controller.cancel();
      
      
       }
    
}

 

 

the is i passed in the test method is a custom object record with the email existing in lead. so the trigger is covering only the lead updation part.

the trigger code is below.

 

 

trigger LeadUpdate on guestuser__c (after update) {

//guestuser__c gu = new guestuser__c();
//list<guestuser__c> gst =[];
list<Lead> newlead = [select id,email,phone,title,HasOptedOutOfEmail,A_H_Interest__c,Aftermarket_Interest__c,Construction_Interest__c,D_O_Interest__c,Energy_Interest__c,Environmental_Interest__c,Financial_Enterprises_Interest__c,Healthcare_Interest__c,Property_Interest__c,Real_Estate_Interest__c,Manufacturing_Interest__c,Technology_Interest__c,Windstorm_Notification__c from Lead where email=:Trigger.new[0].Email__c];
list<Contact>newcon = [select id,email,phone,title,HasOptedOutOfEmail,A_H_Interest__c,Aftermarket_Interest__c,Construction_Interest__c,D_O_Interest__c,Energy_Interest__c,Environmental_Interest__c,Financial_Enterprises_Interest__c,Healthcare_Interest__c,Property_Interest__c,Real_Estate_Interest__c,Manufacturing_Interest__c,Technology_Interest__c,Windstorm_Notification__c from Contact where email=:Trigger.new[0].Email__c];
if(newlead.size()>0)
{
newlead[0].Email = Trigger.new[0].Email__c;
newlead[0].Phone = Trigger.new[0].Phone__c;
newlead[0].title = Trigger.new[0].Title__c;
newlead[0].HasOptedOutOfEmail = Trigger.new[0].Email_Optout__c;
newlead[0].A_H_Interest__c = Trigger.new[0].A_H_interests__c;
newlead[0].D_O_Interest__c = Trigger.new[0].D_O_interests__c;
newlead[0].Real_Estate_Interest__c = Trigger.new[0].RealEstate_interests__c;
newlead[0].Property_Interest__c = Trigger.new[0].Property_interests__c;
newlead[0].Healthcare_Interest__c = Trigger.new[0].Healthcare_interests__c;
newlead[0].Aftermarket_Interest__c = Trigger.new[0].After_Markets_interests__c;
newlead[0].Construction_Interest__c = Trigger.new[0].Construction_interests__c;
newlead[0].Energy_Interest__c = Trigger.new[0].Energy_interests__c;
newlead[0].Financial_Enterprises_Interest__c = Trigger.new[0].Financial_Enterprises_interests__c;
newlead[0].Environmental_Interest__c = Trigger.new[0].Environmental_interests__c;
newlead[0].Manufacturing_Interest__c = Trigger.new[0].Manufacturing_interests__c;
newlead[0].Technology_Interest__c = Trigger.new[0].Technology_interests__c;
newlead[0].Windstorm_Notification__c = Trigger.new[0].Windstorm_notification_interests__c;

update newlead;
}
else
{
  if(newcon.size()>0)
  {
  newcon[0].Email = Trigger.new[0].Email__c;
    newcon[0].Phone = Trigger.new[0].Phone__c;
    newcon[0].title = Trigger.new[0].Title__c;
    newcon[0].HasOptedOutOfEmail = Trigger.new[0].Email_Optout__c;
    newcon[0].A_H_Interest__c = Trigger.new[0].A_H_interests__c;
    newcon[0].D_O_Interest__c = Trigger.new[0].D_O_interests__c;
    newcon[0].Real_Estate_Interest__c = Trigger.new[0].RealEstate_interests__c;
    newcon[0].Property_Interest__c = Trigger.new[0].Property_interests__c;
    newcon[0].Healthcare_Interest__c = Trigger.new[0].Healthcare_interests__c;
    newcon[0].Aftermarket_Interest__c = Trigger.new[0].After_Markets_interests__c;
    newcon[0].Construction_Interest__c = Trigger.new[0].Construction_interests__c;
    newcon[0].Energy_Interest__c = Trigger.new[0].Energy_interests__c;
    newcon[0].Financial_Enterprises_Interest__c = Trigger.new[0].Financial_Enterprises_interests__c;
    newcon[0].Environmental_Interest__c = Trigger.new[0].Environmental_interests__c;
    newcon[0].Manufacturing_Interest__c = Trigger.new[0].Manufacturing_interests__c;
    newcon[0].Technology_Interest__c = Trigger.new[0].Technology_interests__c;
    newcon[0].Windstorm_Notification__c = Trigger.new[0].Windstorm_notification_interests__c;
  update newcon;  
  }
}
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Anand@SAASAnand@SAAS

Relying on existing data in the Org  for yout test methods is a very bad practice. You should create the Lead/Contact objects in your test method and then invoke the apex class/trigger and assert the results.  Refer to http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm for some good practices on test methods and test coverage.

All Answers

Anand@SAASAnand@SAAS

Relying on existing data in the Org  for yout test methods is a very bad practice. You should create the Lead/Contact objects in your test method and then invoke the apex class/trigger and assert the results.  Refer to http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm for some good practices on test methods and test coverage.

This was selected as the best answer
krishnagkrishnag

i know its a bad practice and in my case i am writing a test class for trigger. i wrote the test class for controller and for the controller i dont know if i u saw my code or not its populating the data and i am modifying the populated data .anyway thanks for ur suggestion and reference.

krishnagkrishnag

sorry  typo mistake in above post "i am not writing a test class for trigger"

krishnagkrishnag

thanks aanand i tried your suggestion and it worked fine.but lot of dependencies when we insert objetcs the related objects triggers are firing up and test coverage is gettign down. Anyway i did some workaround and got the test coverage till 90% and deployed,

--cheers.