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
Semira@gmail.comSemira@gmail.com 

help with code coverage

Hi All, 

I wrote a custom controller and now trying to increase the code coverage. It is stuck at 39%. I'm not sure what am I missing? 

public String SelectPrimaryAccount { get; set; }
    public boolean SameAsCaller { get; set; }

 
    public List<SelectOption> getPrimaryAccount() {
        List<SelectOption> primaryAccount = new List<SelectOption>();
        primaryAccount.add(new SelectOption('','-None-'));
        primaryAccount.add(new SelectOption('Caller Account','Caller Account'));
        primaryAccount.add(new SelectOption('Project Site Contact', 'Project Site Contact'));
        primaryAccount.add(new SelectOption('Insurance', 'Insurance'));
 
        return primaryAccount;
    }

    public PageReference Save() {
    
        //Job Account and contact   
            Id acctid = Job.Account__c;
            id conid;
            
            job_Contact__c c1; job_Account__c a1;
            for(Contact con : [select id, accountid from Contact where id =:Job.Account__c]){
                c1 = new job_Contact__c(contact__c = con.id);
                a1 = new Job_Account__c(account__c = con.accountid);
                conid = con.id;
                Job.Account__c = con.accountid;
                
                if(SelectPrimaryAccount == 'Caller Account'){
                            job.account__c = con.accountid;
                }
            }
            
            job_Contact__c c2; job_Account__c a2; Contact con2;
            if(SameAsCaller != true && job.Project_Site_Contact_Name__c != null){
            
                con2 = [select id, accountid from Contact where id =:job.Project_Site_Contact_Name__c limit 1];
                job.Project_Site_Contact_Account__c = con2.accountid;
                
                c2 = new job_Contact__c(contact__c = con2.id);
                a2 = new Job_Account__c(account__c = con2.accountid);
                
                if(SelectPrimaryAccount == 'Project Site Contact'){
                        job.contact__c = job.Project_Site_Contact_Name__c;
                        job.account__c = con2.accountid;
                }
            }
            else {
                job.Project_Site_Contact_Name__c = conid;
                job.Project_Site_Contact_Account__c = job.account__c;
                //job.account__c = conid.accountid;
            }
            
            job_Contact__c c3; job_Account__c a3; Contact con3;
            if(job.Insurance_Adjuster_Broker__c != null){
                con3 = [select id, accountid from Contact where id =:job.Insurance_Adjuster_Broker__c limit 1];
                c3 = new job_Contact__c(contact__c = con3.id);
                a3 = new Job_Account__c(account__c = con3.accountid);
                   
                if(SelectPrimaryAccount == 'Insurance'){
                        job.contact__c = job.Insurance_Adjuster_Broker__c;
                        job.account__c = con3.accountid;
                }
            }
            
            if(acctid != null){
                a3 = new Job_Account__c(account__c = acctid);
                if(SelectPrimaryAccount == 'Insurance'){
                    job.account__c = job.account__c;
                    job.contact__c = null;}
            
            }
            
        
        insert job; 
        
           if(c1!=null){
            c1.job__c = job.id;
            a1.job__c = job.id;
            jcon.add(c1);jacct.add(a1);}
            
            if(c2!=null){
                c2.job__c = job.id;
                a2.job__c = job.id;
                jcon.add(c2); jacct.add(a2);
            }
        
            if(acctid!=null){
                a3.job__c = job.id;
                jacct.add(a3);
                if(c3!=null){
                    c3.job__c = job.id;
                    jcon.add(c3);
                }
            }
          
            insert jCon;
            insert jAcct;

It's getting coverage on the queries and the confitional statements. But that's about it. 

Here's my test class:

@isTest (seealldata = true)
                    
public class CreateNewJobInquiry_Test {

    public static testMethod void TestCreateNewJobInquiry () {
        //PageReference pageRef = page.NewRecord;
        //Test.setCurrentPage(pageRef);
      
        Contact contact = new Contact();
        Account account = new account(billingStreet = contact.mailingstreet, billingcity = contact.mailingCity, billingstate = contact.mailingcity, billingpostalcode = contact.mailingpostalcode, phone = contact.phone);
        account.name = 'CIG';
        insert account;
        
        Contact.contact_type__c = 'Adjustor';
        contact.firstname = 'semira';
        contact.Lastname = 'roy';
        contact.accountid = account.id;
        insert contact;
    
        Contact contact2 = new Contact();
        Contact2.contact_type__c = 'Adjustor';
        contact2.Lastname = 'noname';
        contact2.firstname = 'noname';
        contact2.accountid = account.id;
        insert contact2;
        
        
    Job__c job = new Job__c(account__c = account.id, Job_Name__c = 'test', Project_Site_Address__c = contact.mailingstreet, Project_Site_City__c = 'Los Angeles', Project_Site_state__c = 'CA', Project_Site_zipcode__c ='90027', County__c = 'LA', City_of_LA__c = 'Yes');
    job.contact__c = contact.id;
    job.Project_Site_Contact_Name__c = contact2.id;
    job.Insurance_Adjuster_Broker__c = contact.id;
    
    
    ApexPages.currentPage().getParameters().put('qp', job.id);
    CreateNewJobInquiry testObj2 = new CreateNewJobInquiry();
    testObj2.Sameascaller = false;
    testObj2.SelectPrimaryAccount = 'caller account';
    
        testObj2.save();
    }
}


Best Answer chosen by Semira@gmail.com
Gigi.OchoaGigi.Ochoa
In your class, is Job a public variable?  

Add this before testObje2.save()
testObj2.Job = job;

Also do another test methods for all the other Primary Account Options


All Answers

Gigi.OchoaGigi.Ochoa
In the developer console, run all your test again.  Then open the class you are lacking in code coverage.  You will then be able to view which lines of code were not covered in your test methods.  You can also view which lines in your class are not covered in Eclipse as well in the Apex Test Runner tab.

Just briefly looking at your code, make sure that write test method(s) that can handle all your if/then scenarios.

Here is more information on test methods best practices
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm
Semira@gmail.comSemira@gmail.com
@Gigi.Ochoa I did run my code in Developer console that's how I know which lines are covering. What I can't figure out what I should be writing to check these lines. 

There is a big chunk that is not getting covered and why I posted that big chunk of my controller.
Gigi.OchoaGigi.Ochoa
In your class, is Job a public variable?  

Add this before testObje2.save()
testObj2.Job = job;

Also do another test methods for all the other Primary Account Options


This was selected as the best answer
Semira@gmail.comSemira@gmail.com
@Gigi.Ochoa job is our custom object. But I see what you mean. I didn't even pass the job through the controller. Duh! it raised to 63% which is an improvement. I will try to work from here. Thank you.