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
DTRain521DTRain521 

Test class at 72% need small help to increase

Hi all,

The following code is almost covered but I need some help with covering some of the code that I dont know how to right coverage for.  Below is the class and test class in the class there is reference to disablexxx = true; and xxxQuoteValue = true;  I need to cover these in my test class.  There is an attempt to cover in the test class but something is not written right.

 

Any help would be great.

 

Class:  disableAuto and such refers to items on my vf page.

 

        if(l.Consumer_Products__c == auto)
        {
            disableAuto = true;
            disableHome = true;
            disableUmbrella = true;
            disableBoat = true;
            disableDwellingFire = true;
            disableThirdParty = true;
            autoQuoteValue = true;
        }
        

 

 

test class:  note the "clc.autoQuoteValue = true;"  running test shows that it is not covered.

 

ApexPages.currentPage().getParameters().put('id', lead.Id);
            ApexPages.StandardController sc = new ApexPages.StandardController(lead);
            Test.startTest();
    
            ConvertLeadController clc = new ConvertLeadController(sc);
            clc.autoQuoteValue = true;
            clc.boatQuoteValue = true;
            clc.dwellingFireQuoteValue = true;
            clc.homeQuoteValue = true;
            clc.thirdPartyQuoteValue = true;
            clc.convertLead();
            

 Thanks in Advance,

Dwayne

 

Best Answer chosen by Admin (Salesforce Developers) 
Kevin BromerKevin Bromer

If you have further 'Consumer_Products__c ==' statements you need covered, then yes, you'll need to insert/update a lead for each execution path (each 'if' statement)

All Answers

Kevin BromerKevin Bromer

Hey Dwayne-

 

Could you post some additional code? Looking at your test class, it doesn't appear you ever set the field l.Consumer_Products__c equal to 'auto'.  So this line in the test class:

 

  if(l.Consumer_Products__c == auto)

 

appears as if it would return false.  

 

 

 

 

DTRain521DTRain521

Kevin,

Thanks for taking a look.  Here is the first part of the test class up to and including the part I need help with.

 

 

public class ConvertLeadController
{
    List <Opportunity> oppsToUpdate = new List <Opportunity>();
    public Lead l {get; set;}
    private final ApexPages.StandardController lsc = null;
    
    private Id autoQuoteRecordTypeId;
    private Id homeQuoteRecordTypeId;
    private Id umbrellaQuoteRecordTypeId;
    private Id dwellingFireQuoteRecordTypeId;
    private Id boatQuoteRecordTypeId;
    private Id thirdPartyQuoteRecordTypeId;
    
    final static String auto = 'Auto';
    final static String home = 'Home';
    final static String umbrella = 'Umbrella';
    final static String boat = 'Boat';
    final static String dwellingFire = 'Dwelling/Fire';
    final static String thirdParty = 'Third Party';
    
    public ConvertLeadController(ApexPages.StandardController sc) 
    {
        setRecordTypes();
        
        this.l = (Lead)sc.getRecord();
        this.lsc = sc;
        if(this.l.Id == null)
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'This page cannot be accessed directly.'));
            isValidLead = false;
            return;
        }
        //Default values
        isValidLead = true;
        
        if(!((String)(l.OwnerId)).startsWith('00G'))
            act.OwnerId = l.OwnerId;
        
        opp.Name = l.Company + '-';
        sendEmail = false;
        doNotCreateOpp = false;
        doNotCreateTask = true;
        renderLookup = true;
        LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
        convertedStatusOptions.add(new SelectOption(convertStatus.Id, convertStatus.MasterLabel));
        accountNameOptions.add(new SelectOption('', '--None--'));
        accountNameOptions.add(new SelectOption('create', 'Create New Account: ' + l.Company));
        
        Lead leadQuery = [SELECT Email, Consumer_Products__c, Billing_Street__c, Billing_City__c, Billing_City_State__c, 
        Billing_State_Province__c, Billing_Zip__c, Current_Policy_Number__c, Account__c, Account__r.Name 
        FROM Lead WHERE Id =: l.Id];
        
        l.Account__c = leadQuery.Account__c;
        l.Consumer_Products__c = leadQuery.Consumer_Products__c;
        l.Billing_Street__c = leadQuery.Billing_Street__c;
        l.Billing_City__c = leadQuery.Billing_City__c;
        l.Billing_City_State__c = leadQuery.Billing_City_State__c;
        l.Billing_State_Province__c = leadQuery.Billing_State_Province__c;
        l.Billing_Zip__c = leadQuery.Billing_Zip__c;
        l.Current_Policy_Number__c = leadQuery.Current_Policy_Number__c;
        
        if(leadQuery.Email != null)
            l.Email = leadQuery.Email;
        if(leadQuery.Billing_Street__c != null)
            l.Billing_Street__c = leadQuery.Billing_Street__c;
        if(leadQuery.Billing_City__c != null)
            l.Billing_City__c = leadQuery.Billing_City__c;
        if(leadQuery.Billing_City_State__c != null)
            l.Billing_City_State__c = leadQuery.Billing_City_State__c;
        if(leadQuery.Billing_Zip__c != null)
            l.Billing_Zip__c = leadQuery.Billing_Zip__c;
        if(leadQuery.Current_Policy_Number__c != null)
            l.Current_Policy_Number__c = leadQuery.Current_Policy_Number__c;
        
        accountName = leadQuery.Account__r.Name;
        
        if(l.Consumer_Products__c == auto)
        {
            disableAuto = true;
            disableHome = true;
            disableUmbrella = true;
            disableBoat = true;
            disableDwellingFire = true;
            disableThirdParty = true;
            autoQuoteValue = true;
        }

 

 

DTRain521DTRain521

Kevin,

So sorry I posted the controller class.  Please see below for the test class.

 

 

@isTest
class TestConvertLeadController
{
    final static String auto = 'Auto';
    final static String home = 'Home';
    final static String umbrella = 'Umbrella';
    final static String boat = 'Boat';
    final static String dwellingFire = 'Dwelling/Fire';
    final static String thirdParty = 'Third Party';    
    
    static testMethod void testConvertLeadController()
    {
        //We run our unit test as this user
        User testSysAdminUser = new User(alias = 'standt', email='standarduser@testorg.com', 
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', 
            localesidkey='en_US', profileId = [select id from profile where name='System Administrator' LIMIT 1].Id, 
            timezonesidkey='America/Los_Angeles', username='ASstandarduser@testorg.com');
            
        insert testSysAdminUser;
        
        System.runAs(testSysAdminUser)
        {
            Lead lead = new lead (LastName = 'TEST1',
                                  Company = 'TEST1',
                                  OwnerId = UserInfo.getUserId(),
                                  Consumer_Products__c = umbrella,
                                  Agent_Code__c = 'TEST1',
                                  Email = 'test@test.com',
                                  Billing_Street__c = '1234 W TEST AVE',
                                  Billing_City_State__c = 'Boston, Ma',
                                  Billing_Zip__c = '01653', 
                                  Billing_State_Province__c = 'MA',
                                  Current_Policy_Number__c = '987654321a',
                                  Billing_City__c = 'Boston');
                                 
            insert lead;
            
            ApexPages.currentPage().getParameters().put('id', lead.Id);
            ApexPages.StandardController sc = new ApexPages.StandardController(lead);
            Test.startTest();
    
            ConvertLeadController clc = new ConvertLeadController(sc);
            clc.autoQuoteValue = true;
            clc.boatQuoteValue = true;
            clc.dwellingFireQuoteValue = true;
            clc.homeQuoteValue = true;
            clc.thirdPartyQuoteValue = true;
            clc.convertLead();
            
            Id accountId = [SELECT ConvertedAccountId FROM Lead WHERE Id =: lead.Id].ConvertedAccountId;
            System.assertEquals(6, [SELECT count() FROM Opportunity WHERE AccountId =: accountId]);
            System.assertEquals(0, [SELECT count() FROM Contact WHERE AccountId =: accountId]);
             
            Lead lead2 = new lead (LastName = 'TEST2',
                                  Company = 'TEST2',
                                  OwnerId = UserInfo.getUserId(),
                                  Consumer_Products__c = home,
                                  Agent_Code__c = 'TEST2',
                                  Email = 'test@test.com',
                                  Billing_Street__c = '1234 W TEST AVE',
                                  Billing_City_State__c = 'Boston, Ma',
                                  Billing_Zip__c = '01653', 
                                  Billing_State_Province__c = 'MA',
                                  Current_Policy_Number__c = '987654321a',
                                  Billing_City__c = 'Boston');
            insert lead2;
    
            ApexPages.currentPage().getParameters().put('id', lead2.Id);
            sc = new ApexPages.StandardController(lead2);
    
            clc = new ConvertLeadController(sc);
            clc.autoQuoteValue = true;
            clc.boatQuoteValue = true;
            clc.dwellingFireQuoteValue = true;
            clc.umbrellaQuoteValue = true;
            clc.thirdPartyQuoteValue = true;
            clc.convertLead();

 

 

Kevin BromerKevin Bromer

Without knowing what else your'e doing in the test class, it doesn't look like you're ever explicitly setting Consumer_Products__c. 

 

You can drop a system.debug in before that if statement, something like:

 

system.debug('Consumer_Products__c==================' + l.Consumer_Products__c);

 

and take a look in the debug log and see what the value is before the conditional (IF) fires.

 

 

Kevin BromerKevin Bromer

Yup, its never equal to the static variable 'auto', so its never going to fire.

DTRain521DTRain521

ahh the light bulb goes on.  I guess my next question would be, since it needs to test again 6 difference possible instances of Consumer_Product, if I add the Auto would that cover the other potential instances?

 

Kevin BromerKevin Bromer

If you have further 'Consumer_Products__c ==' statements you need covered, then yes, you'll need to insert/update a lead for each execution path (each 'if' statement)

This was selected as the best answer
DTRain521DTRain521

Yes light bulb is as bright as the sun.  Thank you so much for your help.  I will added the needed code to the test class.  

karthikarthi
Please post the code which is worked out