+ Start a Discussion
Terri HenryTerri Henry 

Test Class coverage at 76%

Racking my brains as to why I can't get the final two lines to pass and hit 100% coverage

It's both branches of the IF statement outstanding, but I thought I had captured that with generating 2 different account records

Any pointers much appreciated!

CLASS
public without sharing class lastViewedAccount{
    public Datetime cDT;
    public String LongDate;
    public String firstname;
    public String lastname;
    public String existinglastviewed;
    public String userid;
    private final Account acc;
    
    public lastViewedAccount(ApexPages.StandardController stdController) {
        this.acc = (Account)stdController.getRecord();
    }
    
    public String getLongDate() {
        cDT = System.now();
        //Formats the datetime value to locale
        LongDate = cDT.format('dd/MM/yyyy HH:mm');
        return LongDate;
    }
    
    
    public void updateField() {
        //Get the user info from the current user
        firstname = System.Userinfo.getFirstName();
        lastname = System.Userinfo.getLastName();
        userid = System.Userinfo.getUserId();
        //Get the Opportunity record to be updated
        Account a = [select Last_Viewed_By__c, Track_Record__c from Account where id = :acc.id limit 1];
        //get existing string of the record, so that new view can be appended to the end of this
        existinglastviewed = a.Last_Viewed_By__c;
        //check to see if the Opportunity has been flagged for tracking
        if(a.Track_Record__c){
            //Assign values to Last Viewed By field and update the record - only concatenate if there is a value there already
            if(existinglastviewed != null){
                a.Last_Viewed_By__c = (existinglastviewed + '; \n' + firstname + ' ' + lastname + ' ' + getLongDate());
            }
            else a.Last_Viewed_By__c = (firstname + ' ' + lastname + ' ' + getLongDate());
            update a;
        }    
    }
    
}

TEST CLASS
@isTest
public with sharing class lastViewedAccountTest {


    static testMethod void triggerTest() {
        
        //Create Account1 - with pre-existing 'Last Viewed By' populated - to test if statement
        Account testAccount1 = new Account(Name = 'testAccount');
        testAccount1.Company_Registered_Number_SOC__c = '00000000';
        testAccount1.Primary_Sector__c = 'Business Goods & Services';
        testAccount1.Sub_Sector__c = 'Point of Sale';
        testAccount1.Organisation_type_SOC__c = 'Limited Company'; 
        testAccount1.Street_Head_Office_SOC__c = 'Test Street'; 
        testAccount1.City_Head_Office_SOC__c = 'London'; 
        testAccount1.Country_Head_Office_SOC__c = 'United Kingdom';
        testAccount1.Last_Viewed_By__c = 'Example User';
        insert testAccount1;
 
 
        ApexPages.StandardController sc1 = new ApexPages.StandardController(testAccount1);
        lastViewedAccount lastViewedAcc1 = new lastViewedAccount(sc1);
        
        System.assert(!String.isEmpty(lastViewedAcc1.getLongDate()));
        lastViewedAcc1.updateField();
        
        
              
        //Create Opportunity 2 - with no 'Last Viewed By' data - to check 2nd branch of if statement
        Account testAccount2 = new Account(Name = 'testAccount');
        testAccount2.Company_Registered_Number_SOC__c = '00000000';
        testAccount2.Primary_Sector__c = 'Business Goods & Services';
        testAccount2.Sub_Sector__c = 'Point of Sale';
        testAccount2.Organisation_type_SOC__c = 'Limited Company'; 
        testAccount2.Street_Head_Office_SOC__c = 'Test Street'; 
        testAccount2.City_Head_Office_SOC__c = 'London'; 
        testAccount2.Country_Head_Office_SOC__c = 'United Kingdom';
        insert testAccount2;
 
 
        ApexPages.StandardController sc2 = new ApexPages.StandardController(testAccount2);
        lastViewedAccount lastViewedAcc2 = new lastViewedAccount(sc2);
        
        System.assert(!String.isEmpty(lastViewedAcc2.getLongDate()));
        lastViewedAcc2.updateField();
        
        
    }   

}

Best Answer chosen by Terri Henry
v varaprasadv varaprasad
Hi, 

Please try once below code : 

Need to add testAccount1.Track_Record__c = True;
 
@isTest
public with sharing class lastViewedAccountTest {


    static testMethod void triggerTest() {
        
        //Create Account1 - with pre-existing 'Last Viewed By' populated - to test if statement
        Account testAccount1 = new Account(Name = 'testAccount');
        testAccount1.Company_Registered_Number_SOC__c = '00000000';
        testAccount1.Primary_Sector__c = 'Business Goods & Services';
        testAccount1.Sub_Sector__c = 'Point of Sale';
        testAccount1.Organisation_type_SOC__c = 'Limited Company'; 
        testAccount1.Street_Head_Office_SOC__c = 'Test Street'; 
        testAccount1.City_Head_Office_SOC__c = 'London'; 
        testAccount1.Country_Head_Office_SOC__c = 'United Kingdom';
        testAccount1.Last_Viewed_By__c = 'Example User';
		testAccount1.Track_Record__c = True;
        insert testAccount1;
 
 
        ApexPages.StandardController sc1 = new ApexPages.StandardController(testAccount1);
        lastViewedAccount lastViewedAcc1 = new lastViewedAccount(sc1);
        
        System.assert(!String.isEmpty(lastViewedAcc1.getLongDate()));
        lastViewedAcc1.updateField();
        
        
              
        //Create Opportunity 2 - with no 'Last Viewed By' data - to check 2nd branch of if statement
        Account testAccount2 = new Account(Name = 'testAccount');
        testAccount2.Company_Registered_Number_SOC__c = '00000000';
        testAccount2.Primary_Sector__c = 'Business Goods & Services';
        testAccount2.Sub_Sector__c = 'Point of Sale';
        testAccount2.Organisation_type_SOC__c = 'Limited Company'; 
        testAccount2.Street_Head_Office_SOC__c = 'Test Street'; 
        testAccount2.City_Head_Office_SOC__c = 'London'; 
        testAccount2.Country_Head_Office_SOC__c = 'United Kingdom';
        testAccount2.Track_Record__c = True;
		insert testAccount2;
 
 
        ApexPages.StandardController sc2 = new ApexPages.StandardController(testAccount2);
        lastViewedAccount lastViewedAcc2 = new lastViewedAccount(sc2);
        
        System.assert(!String.isEmpty(lastViewedAcc2.getLongDate()));
        lastViewedAcc2.updateField();
        
        
    }   

}

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For  Support: varaprasad4sfdc@gmail.com
Blog : http://salesforceprasad.blogspot.com/


 

All Answers

v varaprasadv varaprasad
Hi, 

Please try once below code : 

Need to add testAccount1.Track_Record__c = True;
 
@isTest
public with sharing class lastViewedAccountTest {


    static testMethod void triggerTest() {
        
        //Create Account1 - with pre-existing 'Last Viewed By' populated - to test if statement
        Account testAccount1 = new Account(Name = 'testAccount');
        testAccount1.Company_Registered_Number_SOC__c = '00000000';
        testAccount1.Primary_Sector__c = 'Business Goods & Services';
        testAccount1.Sub_Sector__c = 'Point of Sale';
        testAccount1.Organisation_type_SOC__c = 'Limited Company'; 
        testAccount1.Street_Head_Office_SOC__c = 'Test Street'; 
        testAccount1.City_Head_Office_SOC__c = 'London'; 
        testAccount1.Country_Head_Office_SOC__c = 'United Kingdom';
        testAccount1.Last_Viewed_By__c = 'Example User';
		testAccount1.Track_Record__c = True;
        insert testAccount1;
 
 
        ApexPages.StandardController sc1 = new ApexPages.StandardController(testAccount1);
        lastViewedAccount lastViewedAcc1 = new lastViewedAccount(sc1);
        
        System.assert(!String.isEmpty(lastViewedAcc1.getLongDate()));
        lastViewedAcc1.updateField();
        
        
              
        //Create Opportunity 2 - with no 'Last Viewed By' data - to check 2nd branch of if statement
        Account testAccount2 = new Account(Name = 'testAccount');
        testAccount2.Company_Registered_Number_SOC__c = '00000000';
        testAccount2.Primary_Sector__c = 'Business Goods & Services';
        testAccount2.Sub_Sector__c = 'Point of Sale';
        testAccount2.Organisation_type_SOC__c = 'Limited Company'; 
        testAccount2.Street_Head_Office_SOC__c = 'Test Street'; 
        testAccount2.City_Head_Office_SOC__c = 'London'; 
        testAccount2.Country_Head_Office_SOC__c = 'United Kingdom';
        testAccount2.Track_Record__c = True;
		insert testAccount2;
 
 
        ApexPages.StandardController sc2 = new ApexPages.StandardController(testAccount2);
        lastViewedAccount lastViewedAcc2 = new lastViewedAccount(sc2);
        
        System.assert(!String.isEmpty(lastViewedAcc2.getLongDate()));
        lastViewedAcc2.updateField();
        
        
    }   

}

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For  Support: varaprasad4sfdc@gmail.com
Blog : http://salesforceprasad.blogspot.com/


 
This was selected as the best answer
Steven NsubugaSteven Nsubuga
You forgot to set Track_Record__c = true in your test accounts
@isTest
public with sharing class lastViewedAccountTest {


    static testMethod void triggerTest() {
        
        //Create Account1 - with pre-existing 'Last Viewed By' populated - to test if statement
        Account testAccount1 = new Account(Name = 'testAccount');
        testAccount1.Company_Registered_Number_SOC__c = '00000000';
        testAccount1.Primary_Sector__c = 'Business Goods & Services';
        testAccount1.Sub_Sector__c = 'Point of Sale';
        testAccount1.Organisation_type_SOC__c = 'Limited Company'; 
        testAccount1.Street_Head_Office_SOC__c = 'Test Street'; 
        testAccount1.City_Head_Office_SOC__c = 'London'; 
        testAccount1.Country_Head_Office_SOC__c = 'United Kingdom';
        testAccount1.Last_Viewed_By__c = 'Example User';
        testAccount1.Track_Record__c = true;
        insert testAccount1;
 
 
        ApexPages.StandardController sc1 = new ApexPages.StandardController(testAccount1);
        lastViewedAccount lastViewedAcc1 = new lastViewedAccount(sc1);
        
        System.assert(!String.isEmpty(lastViewedAcc1.getLongDate()));
        lastViewedAcc1.updateField();
        
        
              
        //Create Opportunity 2 - with no 'Last Viewed By' data - to check 2nd branch of if statement
        Account testAccount2 = new Account(Name = 'testAccount');
        testAccount2.Company_Registered_Number_SOC__c = '00000000';
        testAccount2.Primary_Sector__c = 'Business Goods & Services';
        testAccount2.Sub_Sector__c = 'Point of Sale';
        testAccount2.Organisation_type_SOC__c = 'Limited Company'; 
        testAccount2.Street_Head_Office_SOC__c = 'Test Street'; 
        testAccount2.City_Head_Office_SOC__c = 'London';
        testAccount2.Track_Record__c = true;
        testAccount2.Country_Head_Office_SOC__c = 'United Kingdom';
        insert testAccount2;
 
 
        ApexPages.StandardController sc2 = new ApexPages.StandardController(testAccount2);
        lastViewedAccount lastViewedAcc2 = new lastViewedAccount(sc2);
        
        System.assert(!String.isEmpty(lastViewedAcc2.getLongDate()));
        lastViewedAcc2.updateField();
        
        
    }   

}



 
Terri HenryTerri Henry
Of course!

Thanks Varaprasad and Steven