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
kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12 

Trigger code coverage for test class

Hi all,

I am written one trigger and test class.. Test class code covered 73% i need to increase more.
Trigger:-
trigger UpdatePropertybyGroup on Property__c (after insert, after update) {
    
    Set<Object> Properties = new set<Object>();
    Map< String, Set<Object> > PropertiesPerAccount = new Map< String, Set<Object> >();
    Map< String, Object> AllProperties = new Map<String, Object>();
    Set< String> AllIdsOfproperties = new Set<String>();
    List<Property__c> NeedToUpdateGroupProperties = new List<Property__c>();
     Property__c p1 = Trigger.new[0];
    
    if(   p1.Account__c == '001f000000l10C5' ) {
    for(Property__c p : Trigger.new){
        AllProperties.put( p.Account__c , p);
        AllIdsOfproperties.add(p.Id);
    }
    set<String>AccountIds = AllProperties.keySet();

    for( String account: AccountIds ){
        Properties.clear();
        for( String propAccount :AllProperties.keySet() ) {
            if( AllProperties.containsKey( account ) )
                Properties.add( AllProperties.get( account ) );
        }
        PropertiesPerAccount.put( account , Properties);
    }
    
    List<Property__c> SFDCproperties = [Select Id,name,Account__c,Group_Membership__c from Property__c where Id In :AllIdsOfproperties];
    List<Grouping__c> SFDCGroups     = [Select Id,name,Account__c from Grouping__c where Account__c IN : AccountIds];
    Map<String, String> NewGroupPerAccount = new Map<String, String>();
    set<String> UniqueGroupName = new set<String>();
    List<Grouping__c> groups      = new List<Grouping__c>();
    
    for(Property__c p :SFDCproperties) {
        if( null == p.Group_Membership__c ) {
            if( ! SFDCGroups.isEmpty() ) {
                Boolean IsGroupExists = false;
                for( Grouping__c grp : SFDCGroups ) {
                    if( grp.Name == p.Name ) {
                        IsGroupExists = true;
                        p.Group_Membership__c = grp.Id;  
                        NeedToUpdateGroupProperties.add(p);
                        break;
                    } else {
                        IsGroupExists = false;
                       
                    }
                }
                
                if( false == IsGroupExists ) {
                    if(!UniqueGroupName.contains(p.Name)){
                        Grouping__c g = new Grouping__c();
                        g.Name = p.Name;
                        g.Account__c = p.Account__c;
                        groups.add(g);
                        UniqueGroupName.add(p.Name);
                    }
                }
            } else {
                if(!UniqueGroupName.contains(p.Name)){
                    Grouping__c g = new Grouping__c();
                    g.Name = p.Name;
                    g.Account__c = p.Account__c;
                    groups.add(g);
                    UniqueGroupName.add(p.Name);
                }
            }
        } 
    }
    
    if( !groups.isEmpty() )
        upsert groups;
    
    for(Property__c p :SFDCproperties){
        for(Grouping__c grp : groups){
            if(grp.Name == p.Name){
                p.Group_Membership__c = grp.Id;  
                NeedToUpdateGroupProperties.add(p);
            }
        }
    }
    if( !NeedToUpdateGroupProperties.isEmpty() )
        update NeedToUpdateGroupProperties;
}
}    

Test class:-
@isTest
public class TestUpdatePropertybyGroup {
    @isTest static void updateproperty() {
       
        Account a = new Account(Name ='Test');
        insert a;
        Property__c p = new Property__c(Name ='Testing', Active__c = 'ýes', Price__c =1,Square_Footage__c = 1,Bed_Count__c =1, Bath_Count__c =1, Pet_Policy__c ='No',Lease_Terms__c = 1,Address_Line_1__c = 'Test', City__c ='Test', State__c='Test', Zip_Code__c ='1',Concurrent_Showings_Allowed__c= 'no', Default_Appointment_Length__c ='no', Default_Buffer_Before_Appointments__c ='no');
        insert p;
        Grouping__c g = new Grouping__c(Name = 'Testing');
        insert g;
        
    }
}

Thanks in adv.
 
Best Answer chosen by kumar.fdc81.3902978579608325E12
Jayant JadhavJayant Jadhav
Kumar,

Try below code I have added account id to each of records i.e property and group. 

@isTest
public class TestUpdatePropertybyGroup 
{
    @isTest static void updateproperty() 
    {
       
        Account a = new Account(Name ='Test');
        insert a;
        Property__c p = new Property__c(Name ='Testing',Account__c=a.id, Active__c = 'ýes', Price__c =1,Square_Footage__c = 1,Bed_Count__c =1, Bath_Count__c =1, Pet_Policy__c ='No',Lease_Terms__c = 1,Address_Line_1__c = 'Test', City__c ='Test', State__c='Test', Zip_Code__c ='1',Concurrent_Showings_Allowed__c= 'no', Default_Appointment_Length__c ='no', Default_Buffer_Before_Appointments__c ='no');
        insert p;
        Grouping__c g = new Grouping__c(Name = 'Testing',Account__c=a.id);
        insert g;
        
    }
    
     @isTest static void updatepropertyElse() 
    {
       
        Account a = new Account(Name ='Test');
        insert a;
        Property__c p = new Property__c(Name ='Testing',Account__c=a.id, Active__c = 'ýes', Price__c =1,Square_Footage__c = 1,Bed_Count__c =1, Bath_Count__c =1, Pet_Policy__c ='No',Lease_Terms__c = 1,Address_Line_1__c = 'Test', City__c ='Test', State__c='Test', Zip_Code__c ='1',Concurrent_Showings_Allowed__c= 'no', Default_Appointment_Length__c ='no', Default_Buffer_Before_Appointments__c ='no');
        insert p;
        Grouping__c g = new Grouping__c(Name = 'Kumar',Account__c=a.id);
        insert g;
        
    }
}

All Answers

Jayant JadhavJayant Jadhav
Hi Kumar,

I went through your code and found that you are not following best practices to write triggers.
  1. You trigger is not implemented to handle bulk data load.
  2. Hard code of Account Id ( if(   p1.Account__c == '001f000000l10C5' ) )
  3. Apex trigger context variables are not used.
 But still if you want to use same code then you can use 
// All test methods in this class can access all data. @isTest(SeeAllData=true) instead of @isTest. 
Note : While writting test classes we should always prefer using the @isTest 

https://developer.salesforce.com/page/Apex_Code_Best_Practices (link for best practices example)

Let me know if you need more help on this. Plz mark best answer if this solved your issue.
kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12
Hi jayant,

This is my actual code....
trigger UpdatePropertybyGroup on Property__c (after insert, after update) {
    
    Set<Object> Properties = new set<Object>();
    Map< String, Set<Object> > PropertiesPerAccount = new Map< String, Set<Object> >();
    Map< String, Object> AllProperties = new Map<String, Object>();
    Set< String> AllIdsOfproperties = new Set<String>();
    List<Property__c> NeedToUpdateGroupProperties = new List<Property__c>();
   
    for(Property__c p : Trigger.new){
        AllProperties.put( p.Account__c , p);
        AllIdsOfproperties.add(p.Id);
    }
    set<String>AccountIds = AllProperties.keySet();

    for( String account: AccountIds ){
        Properties.clear();
        for( String propAccount :AllProperties.keySet() ) {
            if( AllProperties.containsKey( account ) )
                Properties.add( AllProperties.get( account ) );
        }
        PropertiesPerAccount.put( account , Properties);
    }
    
    List<Property__c> SFDCproperties = [Select Id,name,Account__c,Group_Membership__c from Property__c where Id In :AllIdsOfproperties];
    List<Grouping__c> SFDCGroups     = [Select Id,name,Account__c from Grouping__c where Account__c IN : AccountIds];
    Map<String, String> NewGroupPerAccount = new Map<String, String>();
    set<String> UniqueGroupName = new set<String>();
    List<Grouping__c> groups      = new List<Grouping__c>();
    
    for(Property__c p :SFDCproperties) {
        if( null == p.Group_Membership__c ) {
            if( ! SFDCGroups.isEmpty() ) {
                Boolean IsGroupExists = false;
                for( Grouping__c grp : SFDCGroups ) {
                    if( grp.Name == p.Name ) {
                        IsGroupExists = true;
                        p.Group_Membership__c = grp.Id;  
                        NeedToUpdateGroupProperties.add(p);
                        break;
                    } else {
                        IsGroupExists = false;
                       
                    }
                }
                
                if( false == IsGroupExists ) {
                    if(!UniqueGroupName.contains(p.Name)){
                        Grouping__c g = new Grouping__c();
                        g.Name = p.Name;
                        g.Account__c = p.Account__c;
                        groups.add(g);
                        UniqueGroupName.add(p.Name);
                    }
                }
            } else {
                if(!UniqueGroupName.contains(p.Name)){
                    Grouping__c g = new Grouping__c();
                    g.Name = p.Name;
                    g.Account__c = p.Account__c;
                    groups.add(g);
                    UniqueGroupName.add(p.Name);
                }
            }
        } 
    }
    
    if( !groups.isEmpty() )
        upsert groups;
    
    for(Property__c p :SFDCproperties){
        for(Grouping__c grp : groups){
            if(grp.Name == p.Name){
                p.Group_Membership__c = grp.Id;  
                NeedToUpdateGroupProperties.add(p);
            }
        }
    }
    if( !NeedToUpdateGroupProperties.isEmpty() )
        update NeedToUpdateGroupProperties;
}    
Jayant JadhavJayant Jadhav
Kumar,

Try below code I have added account id to each of records i.e property and group. 

@isTest
public class TestUpdatePropertybyGroup 
{
    @isTest static void updateproperty() 
    {
       
        Account a = new Account(Name ='Test');
        insert a;
        Property__c p = new Property__c(Name ='Testing',Account__c=a.id, Active__c = 'ýes', Price__c =1,Square_Footage__c = 1,Bed_Count__c =1, Bath_Count__c =1, Pet_Policy__c ='No',Lease_Terms__c = 1,Address_Line_1__c = 'Test', City__c ='Test', State__c='Test', Zip_Code__c ='1',Concurrent_Showings_Allowed__c= 'no', Default_Appointment_Length__c ='no', Default_Buffer_Before_Appointments__c ='no');
        insert p;
        Grouping__c g = new Grouping__c(Name = 'Testing',Account__c=a.id);
        insert g;
        
    }
    
     @isTest static void updatepropertyElse() 
    {
       
        Account a = new Account(Name ='Test');
        insert a;
        Property__c p = new Property__c(Name ='Testing',Account__c=a.id, Active__c = 'ýes', Price__c =1,Square_Footage__c = 1,Bed_Count__c =1, Bath_Count__c =1, Pet_Policy__c ='No',Lease_Terms__c = 1,Address_Line_1__c = 'Test', City__c ='Test', State__c='Test', Zip_Code__c ='1',Concurrent_Showings_Allowed__c= 'no', Default_Appointment_Length__c ='no', Default_Buffer_Before_Appointments__c ='no');
        insert p;
        Grouping__c g = new Grouping__c(Name = 'Kumar',Account__c=a.id);
        insert g;
        
    }
}
This was selected as the best answer
kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12
Hi Jayant,

But it's 73%  only.

Thanks
Jayant JadhavJayant Jadhav
Kumar,

Can you please let me know which section is not covered by test class. If possible attach snap shot.
Jayant JadhavJayant Jadhav
Kumar,

Try this code 

@isTest
public class TestUpdatePropertybyGroup 
{
    @isTest static void updateproperty() 
    {
       
        Account a = new Account(Name ='Test');
        insert a;

      Grouping__c g = new Grouping__c(Name = 'Testing',Account__c=a.id);
        insert g;

        Property__c p = new Property__c(Name ='Testing',Account__c=a.id, Active__c = 'ýes', Price__c =1,Square_Footage__c = 1,Bed_Count__c =1, Bath_Count__c =1, Pet_Policy__c ='No',Lease_Terms__c = 1,Address_Line_1__c = 'Test', City__c ='Test', State__c='Test', Zip_Code__c ='1',Concurrent_Showings_Allowed__c= 'no', Default_Appointment_Length__c ='no', Default_Buffer_Before_Appointments__c ='no');
        insert p;
        
        
    }
    
     @isTest static void updatepropertyElse() 
    {
       
        Account a = new Account(Name ='Test');
        insert a;
     Grouping__c g = new Grouping__c(Name = 'Kumar',Account__c=a.id);
        insert g;  
        Property__c p = new Property__c(Name ='Testing',Account__c=a.id, Active__c = 'ýes', Price__c =1,Square_Footage__c = 1,Bed_Count__c =1, Bath_Count__c =1, Pet_Policy__c ='No',Lease_Terms__c = 1,Address_Line_1__c = 'Test', City__c ='Test', State__c='Test', Zip_Code__c ='1',Concurrent_Showings_Allowed__c= 'no', Default_Appointment_Length__c ='no', Default_Buffer_Before_Appointments__c ='no');
        insert p;
       
    }
}
Jayant JadhavJayant Jadhav
Kumar,

I have change the order of insting the records in above code i.e first Account , Grouping and property this will make sure the statement 
if(! SFDCGroups.isEmpty()) returns true which help for more code coverage.
 
kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12
Thanks Jayant for your helpful it's increased 90%.