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 

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 =;
                a1 = new Job_Account__c(account__c = con.accountid);
                conid =;
                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 =;
                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 =;
                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; 
            c1.job__c =;
            a1.job__c =;
                c2.job__c =;
                a2.job__c =;
                jcon.add(c2); jacct.add(a2);
                a3.job__c =;
                    c3.job__c =;
            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;
        Contact contact = new Contact();
        Account account = new account(billingStreet = contact.mailingstreet, billingcity = contact.mailingCity, billingstate = contact.mailingcity, billingpostalcode = contact.mailingpostalcode, phone =; = 'CIG';
        insert account;
        Contact.contact_type__c = 'Adjustor';
        contact.firstname = 'semira';
        contact.Lastname = 'roy';
        contact.accountid =;
        insert contact;
        Contact contact2 = new Contact();
        Contact2.contact_type__c = 'Adjustor';
        contact2.Lastname = 'noname';
        contact2.firstname = 'noname';
        contact2.accountid =;
        insert contact2;
    Job__c job = new Job__c(account__c =, 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 =;
    job.Project_Site_Contact_Name__c =;
    job.Insurance_Adjuster_Broker__c =;
    CreateNewJobInquiry testObj2 = new CreateNewJobInquiry();
    testObj2.Sameascaller = false;
    testObj2.SelectPrimaryAccount = 'caller account';

Best Answer chosen by
In your class, is Job a public variable?  

Add this before
testObj2.Job = job;

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

All Answers

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
@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.
In your class, is Job a public variable?  

Add this before
testObj2.Job = job;

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

This was selected as the best answer
@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.