You need to sign in to do that
Don't have an account?
SFHelpMePlease
trigger test class handler
Hello,
I have the below code and now need to write a test class to promote code to production. I have attempted to write a test class but the account trigger is not getting any code coverage. Am I inserting the data in the correct place. Could you help me?
trigger AccountTrigger on Account (before insert, before update)
{
TriggerHandler handler = new TriggerHandler();
if(TriggerHandler.runTriggerOnce) {
TriggerHandler.runTriggerOnce = false;
if(Trigger.IsUpdate) {
handler.accountBeforeUpdate();
}
}
}
public class TriggerHandler
{
public static boolean runTriggerOnce= true;
public void accountBeforeUpdate()
{
accountUpdates();
}
private void accountUpdates()
{
for(Account newAcc: (List<Account>)Trigger.New)
{
List<Other_Opp__c> opps = [Select Id, Name, Type__c From Other_Opp__c WHERE Opp_Name__c = :newAcc.Id ];
if(opps.size() > 0){
For (Other_Opp__c f : opps) {
f.Type__c = newAcc.Field1__c;
}
update opps
}
}
@isTest
private class TriggerHandler_Test{
@isTest static void test1() {
Test.startTest();
Account acc = new Account(Name = 'New Acct');
insert acc;
Opp_Name__c opp = new Opp_Name__c (Name = 'New Opp');
insert opp;
Account a = [Select Id, Name, Field1__c from Account where Id = :acc.Id];
a.Field1__c = 'test1';
update a;
Test.stopTest();
}
}
I have the below code and now need to write a test class to promote code to production. I have attempted to write a test class but the account trigger is not getting any code coverage. Am I inserting the data in the correct place. Could you help me?
trigger AccountTrigger on Account (before insert, before update)
{
TriggerHandler handler = new TriggerHandler();
if(TriggerHandler.runTriggerOnce) {
TriggerHandler.runTriggerOnce = false;
if(Trigger.IsUpdate) {
handler.accountBeforeUpdate();
}
}
}
public class TriggerHandler
{
public static boolean runTriggerOnce= true;
public void accountBeforeUpdate()
{
accountUpdates();
}
private void accountUpdates()
{
for(Account newAcc: (List<Account>)Trigger.New)
{
List<Other_Opp__c> opps = [Select Id, Name, Type__c From Other_Opp__c WHERE Opp_Name__c = :newAcc.Id ];
if(opps.size() > 0){
For (Other_Opp__c f : opps) {
f.Type__c = newAcc.Field1__c;
}
update opps
}
}
@isTest
private class TriggerHandler_Test{
@isTest static void test1() {
Test.startTest();
Account acc = new Account(Name = 'New Acct');
insert acc;
Opp_Name__c opp = new Opp_Name__c (Name = 'New Opp');
insert opp;
Account a = [Select Id, Name, Field1__c from Account where Id = :acc.Id];
a.Field1__c = 'test1';
update a;
Test.stopTest();
}
}
Trigger
Handler
Test
On your code You have created a custom object Other_Opp__c which is having an account lookup named "Opp_Name__c". (I feel you should rename it to account_name , until unless it is not namd Opp Name for any requirement)
Now for the test class code coverage,
You have to execute the maximum number of lines of your Apex handler to get a proper coverage. (On dev console you can check the lines being executed by your test class. On the bottom of Developer console -> Tests Tab --> click on any class and then it will give you the uncovered lines in red.)
As per your test code, You are creating an opportunity but not associating it with the account, so that your class SOQL can retrieve it.
Try the below code :
Thanks
Tejender Mohan