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
anvesh@force.comanvesh@force.com 

Need to increse below test code coverage for simple trigger ?

I had written Test class for below sample trigger code. But  it covered only 56% code coverage. I did all correct. can anyone make it 100% ?

Trigger Code :

trigger AcUpdate on Account (Before Insert,before update) {

for(Account A : Trigger.new){
If( A.SE2_Id__c == '20004')
A.name = 'RBC WEALTH MANAGEMENT';


If( A.SE2_Id__c == '20002' )
A.name = 'JJB HILLIARD LYONS';

If( A.SE2_Id__c == '20000' )
A.name = 'FTB ADVISORS INC';


If( A.SE2_Id__c == '20005' )
A.name = 'BB&T SECURITIES LLC';


If( A.SE2_Id__c == '20003' )
A.name = 'KEY INVESTMENT SERVICES';
}
}


Test Class :

@IsTest
public class AcUpdate_Test{
public static testMethod void main(){

Account ac = new Account ();
            ac.name = 'RBC WEALTH MANAGEMENT';
            ac.Status__c = 'Active';
            ac.SE2_Id__c = '25546';
            ac.Channel__c = 'Bank';
       
            insert ac;
            
            ac.SE2_Id__c = '25346';
            ac.name = 'RBC WEALTH MANAGEMENT1';
            update ac;
            
            Account ac1 = new Account ();
            ac1.name = 'JJB HILLIARD LYONS';
            ac1.Status__c = 'Active';
            ac.SE2_Id__c = '25586';
            ac1.Channel__c = 'Bank';
            insert ac1;
            
            ac.SE2_Id__c = '22546';
            ac1.name = 'RBC WEALTH MANAGEMENT12';
            
            update ac1;
            
            Account ac2 = new Account ();
            ac2.name = 'Test Account3';
            ac2.Status__c = 'Active';
            ac2.SE2_Id__c = '125456';
            ac2.Channel__c = 'Bank';
            insert ac2;
            
            ac2.SE2_Id__c = '12546';
            ac2.name = 'RBCSD WEALTH MANAGEMENT12';
            
            update ac1;
            
            Account ac3 = new Account ();
            ac3.name = 'Test Account4';
            ac3.Status__c = 'Active';
            ac2.SE2_Id__c = '21254';
            ac3.Channel__c = 'Bank';
            insert ac3;
            
            ac2.SE2_Id__c = '15256';
            ac3.name = 'RBC WEALTH MANAGEMENT31';
            update ac3;
            
            Account ac4 = new Account ();
            ac4.name = 'Test Account5';
            ac4.Status__c = 'Active';
            ac4.Channel__c = 'Bank';
            ac2.SE2_Id__c = '8546';
            insert ac4;
            
            ac2.SE2_Id__c = '5740';
            ac4.name = 'RBC WEALTH MANAGEMENT41';
            update ac4;


}
}

 
Bhanu MaheshBhanu Mahesh

Hi Anvesh,

You need to prepare the data which will meet the conditions in the test class to get the complete coverage.
In your data I see that no record has the value for SE2_Id__c field which is has the value in the trigger.

Try the below code
 
@IsTest
public class AcUpdate_Test{
	public static testMethod void main(){

		Account ac = new Account ();
		ac.name = 'TestAccount';
		ac.Status__c = 'Active';
		ac.SE2_Id__c = '20004';
		ac.Channel__c = 'Bank';
		insert ac;
		Account acc = [SELECT Id,Name,SE2_Id__c FROM Account WHERE Id =:ac.Id];
		System.assertEquals('RBC WEALTH MANAGEMENT',acc.Name);
		acc.SE2_Id__c = '20002';
		update acc;
		Account acc1 = [SELECT Id,Name,SE2_Id__c FROM Account WHERE Id =:acc.Id];
		System.assertEquals('JJB HILLIARD LYONS',acc1.Name);
		
		Account ac1 = new Account ();
		ac1.name = 'Test Account2';
		ac1.Status__c = 'Active';
		ac.SE2_Id__c = '20000';
		ac1.Channel__c = 'Bank';
		insert ac1;
		Account acc2 = [SELECT Id,Name,SE2_Id__c FROM Account WHERE Id =:ac1.Id];
		System.assertEquals('FTB ADVISORS INC',acc2.Name);
		
		acc2.SE2_Id__c = '20005';
		update acc2;
		Account acc3 = [SELECT Id,Name,SE2_Id__c FROM Account WHERE Id =:acc2.Id];
		System.assertEquals('BB&T SECURITIES LLC',acc3.Name);
		
		
		Account ac2 = new Account ();
		ac2.name = 'Test Account3';
		ac2.Status__c = 'Active';
		ac2.SE2_Id__c = '20003';
		ac2.Channel__c = 'Bank';
		insert ac2;
		Account acc4 = [SELECT Id,Name,SE2_Id__c FROM Account WHERE Id =:ac2.Id];
		System.assertEquals('KEY INVESTMENT SERVICES',acc4.Name);	
	}
}

Regards,
Bhanu Mahesh