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
Mike FitchMike Fitch 

I need help getting code coverage for my apex class

I've only been able to get 72% code coverage for my apex class. I'm new to apex development. Can someone help me please?

Here is my class:

public class PcPContactInfo {
    public Precall_Plan_2__c c{get; set;}
   
    public PcPContactInfo(ApexPages.StandardController controller)
        {
             c = (Precall_Plan_2__c)controller.getRecord();
        }
   
    public pagereference logout()
        {  
             pagereference p1=new pagereference('/apex/Precall_Plan_Detail_clone?id='+c.get('Id')+'&clone=1');
             return p1;
        }
   
   
    public PageReference dosave()
        {   
             try
            {
                 if(ApexPages.currentPage().getParameters().get('clone')=='1'||ApexPages.currentPage().getParameters().get('save_new')=='1')
                    {
                        c.id=null;                   
                        insert c;
                    }
               
                 else
                        update c;
            }
       
                 catch(exception e) { ApexPages.addMessages(e); return null;  } 

                 return new Pagereference('/' + c.get('Id'));
        }
    
    public void doSomething()
        {
           contact TEST = [SELECT account.name,MailingCountry,Mailingcity,Title,Phone,MailingStreet,MailingState,MailingPostalCode, accountid FROM contact where id = :c.Contact_name2__c];
           // c.Company_Name__c= TEST.account.name;
           c.Contact_mailing_city__c=TEST.Mailingcity;
           c.Contact_s_Title__c=TEST.Title;
           c.Contact_s_Telephone_Number__c=TEST.Phone;
           c.Contact_Street__c=TEST.MailingStreet;
           c.Contact_State__c=TEST.MailingState;
           c.Contact_Zip__c=TEST.MailingPostalCode;
     
        }
}

And here is my Test Class:

@isTest
private class test_PcPContactInfo
{

    static testMethod void go_PcPContactInfo() 
    {
   
        account a = new account(name='ESAB');
        insert a;
        contact c = new contact (lastname= 'test2',accountid=a.id,email='abc@gmail.com');
        insert c;
       
        Precall_Plan_2__c  cw=new Precall_Plan_2__c(name='test',Contact_name2__c=c.id,Company_Name__c=a.id);
        insert cw;
       
        ApexPages.StandardController controller = new ApexPages.StandardController(cw);
        PcPContactInfo stdController = new PcPContactInfo(controller);
        //account a = [SELECT name,id FROM account limit 1];
       
        //contact TEST = [SELECT id, accountid,email FROM contact where id = :cw.contact__c];
        //cw.Email__c = TEST.email;
        //update cw;
       
        stdController.doSomething();
        stdController.dosave();
       
       
    }
}
Best Answer chosen by Mike Fitch
Prabu MahalingamPrabu Mahalingam
Thats better now. :)
Check out I have added some code to your existing test class

@isTest
private class test_PcPContactInfo
{

    static testMethod void go_PcPContactInfo()
    {
  
        account a = new account(name='ESAB');
        insert a;
        contact c = new contact (lastname= 'test2',accountid=a.id,email='abc@gmail.com');
        insert c;
      
        Precall_Plan_2__c  cw=new Precall_Plan_2__c(name='test',Contact_name2__c=c.id,Company_Name__c=a.id);
        insert cw;
      
        ApexPages.StandardController controller = new ApexPages.StandardController(cw);
        PcPContactInfo stdController = new PcPContactInfo(controller);
        //account a = [SELECT name,id FROM account limit 1];
      
        //contact TEST = [SELECT id, accountid,email FROM contact where id = :cw.contact__c];
        //cw.Email__c = TEST.email;
        //update cw;
      
        stdController.doSomething();
        ApexPages.currentPage().getParameters().put('clone','1');
        stdController.dosave();
        stdController.logout();
       
    
      
    }
}

Now I think it will cover the most of the part except the catch part.

Let me know after running the test.


Cheers.

All Answers

Prabu MahalingamPrabu Mahalingam
Hi,
It woul be easy to help if you highlight the lines which are not covered.
Mike FitchMike Fitch
My apologies. I see how that would help. I dont know why i didnt think to do that :). The lines in bold are the ones NOT covered in the test. Let me know if I can provide any other information to assist. Thanks.


public class PcPContactInfo {
    public Precall_Plan_2__c c{get; set;}
   
    public PcPContactInfo(ApexPages.StandardController controller)
        {
             c = (Precall_Plan_2__c)controller.getRecord();
        }
   
    public pagereference logout()
        {  
             pagereference p1=new pagereference('/apex/Precall_Plan_Detail_clone?id='+c.get('Id')+'&clone=1');
             return p1;

        }
   
   
    public PageReference dosave()
        {   
             try
            {
                 if(ApexPages.currentPage().getParameters().get('clone')=='1'||ApexPages.currentPage().getParameters().get('save_new')=='1')
                    {
                        c.id=null;                   
                        insert c;

                    }
               
                 else
                        update c;
            }
       
                 catch(exception e) { ApexPages.addMessages(e); return null;  } 

                 return new Pagereference('/' + c.get('Id'));
        }
    
    public void doSomething()
        {
           contact TEST = [SELECT account.name,MailingCountry,Mailingcity,Title,Phone,MailingStreet,MailingState,MailingPostalCode, accountid FROM contact where id = :c.Contact_name2__c];
           // c.Company_Name__c= TEST.account.name;
           c.Contact_mailing_city__c=TEST.Mailingcity;
           c.Contact_s_Title__c=TEST.Title;
           c.Contact_s_Telephone_Number__c=TEST.Phone;
           c.Contact_Street__c=TEST.MailingStreet;
           c.Contact_State__c=TEST.MailingState;
           c.Contact_Zip__c=TEST.MailingPostalCode;
     
        }
}
Prabu MahalingamPrabu Mahalingam
Thats better now. :)
Check out I have added some code to your existing test class

@isTest
private class test_PcPContactInfo
{

    static testMethod void go_PcPContactInfo()
    {
  
        account a = new account(name='ESAB');
        insert a;
        contact c = new contact (lastname= 'test2',accountid=a.id,email='abc@gmail.com');
        insert c;
      
        Precall_Plan_2__c  cw=new Precall_Plan_2__c(name='test',Contact_name2__c=c.id,Company_Name__c=a.id);
        insert cw;
      
        ApexPages.StandardController controller = new ApexPages.StandardController(cw);
        PcPContactInfo stdController = new PcPContactInfo(controller);
        //account a = [SELECT name,id FROM account limit 1];
      
        //contact TEST = [SELECT id, accountid,email FROM contact where id = :cw.contact__c];
        //cw.Email__c = TEST.email;
        //update cw;
      
        stdController.doSomething();
        ApexPages.currentPage().getParameters().put('clone','1');
        stdController.dosave();
        stdController.logout();
       
    
      
    }
}

Now I think it will cover the most of the part except the catch part.

Let me know after running the test.


Cheers.
This was selected as the best answer
Mike FitchMike Fitch
Yes sir. 90% code coverage!! Thank you so much!!