2

I have a trigger that does something like the following and I am trying to write a test class for it. I am not sure the best way to handle IF else conditions. The if statement basically say if the object has a relationship to an object then do stuff. So I under that that I will need to create the data for Contact and Opportunity which is the look ups that it is checking for. The way I am doing it now is I created two Methods creating the data twice and then creating my object with 1 id and another.

    FOR (Object__c myObject : Trigger.new)
    {
        IF (myObject.Contact__c != null)
        {
             Do stuff
        }

        ELSE IF (myObject.Opportunity2__c != null)
       {
            Do Other Stuff
       }

}

Test Class

@isTest(SeeAllData=true)

Public with sharing class TestUpdateA
{

Static TestMethod Void UpdateMyObject()
 {
 Account bt= new Account
(
    name='Test Bill To',

);

Opportunity o = new Opportunity
(
    name='test',
    AccountId=a.Id
);

insert o;

Contact c = new Contact
(
    FirstName='Steve',
    LastName='Rogers',
    AccountId=a.Id
);
insert c;

SBQQ__Quote__c quote =new SBQQ__Quote__c
(
SBQQ__Opportunity2__c = o.id,

);
insert quote;


}


Static TestMethod Void UpdateMyObject()
{
 Account bt= new Account
(
    name='Test Bill To',

);

Opportunity o = new Opportunity
(
    name='test',
    AccountId=a.Id
);

insert o;

Contact c = new Contact
(
    FirstName='Steve',
    LastName='Rogers',
    AccountId=a.Id
);
insert c;

SBQQ__Quote__c quote =new SBQQ__Quote__c
(
Contact__c = c.id,

);
insert quote;

}

}

I have tried to just insert in two objects but it does hit the else statement for some reason. Also I tried to just build the data and then created a Static Void Method and calling it twice to create the object but that doesnt seem to hit the else statement either. Is there a better way than just duplicating code?

  • 3
    @isTest(SeeAllData=true) this might be the reason. Please make it as false. Unless needed seeAllData is bad practise and discouraged a lot – Pranay Jaiswal May 25 '18 at 14:25
  • 1
    I'm guessing myObject in your code is SBQQ_Quote__c from the test class. Try removing (SeeAllData=true), it is poor practice to do this as you should be creating your test data in the test class, not using org data. – JaredT May 25 '18 at 14:26
  • Ah.... So I could essentially create one method to create the data then call a method to insert my object 2X – Brooke May 25 '18 at 14:29
  • Is there a better way to write this statement even with removing SeeAllData? – Brooke May 25 '18 at 14:34
  • 2
    It's hard for us to help you with test methods when you have pseudo code but then have real test objects.. Post your real code and we can help you out with Testing – EricSSH May 25 '18 at 15:11
2

Take a look at the code I have below. See that in the trigger code, only one of the code blocks under IF{}ELSEIF{}ELSE{} will run for each record. Assuming this is the desired effect, you can test your code like so:

Trigger code

trigger sfse on SBQQ__Quote__c (after insert, after update) {
    for (SBQQ__Quote__c myObject : trigger.new) {
        if (myObject.Contact__c != null) {
            // logic
        } else if (myObject.Opportunity2__c != null) {
            // other logic
        } /* this part is implied -- else {
            // do nothing 
        } */
    }
}

Test class

@isTest
public class testSFSETrigger {
    public static void myTest() {
        Account a1 = new Account();
        a1.Name = 'Test Bill To';
        insert a1;

        Opportunity o1 = new Opportunity();
        o1.Name = 'test';
        o1.Account = a1.Id;
        insert o1;

        Contact c1 = new Contact();
        c1.FirstName = 'Steve';
        c1.LastName = 'Rogers';
        c1.AccountId = a1.Id;
        insert c1;


        //Insert 1 quote to satisfy first fork criteria
        //where myObject.Contact__c != null;
        SBQQ__Quote__c quote = new SBQQ__Quote__c();
        quote.Contact__c = c1.Id;
        insert quote;

        //Insert another quote to satisfy second fork criteria
        // where myObject.Contact__c == NULL
        // and  myObject.Opportunity2__c != null 
        SBQQ__Quote__c quote2 = new SBQQ__Quote__c();
        quote2.Opportunity2__c = o1.Id;
        insert quote2;


        system.assert(/*Check for trigger fork1 desired outcome */);
        system.assert(/*Check for trigger fork2 desired outcome */);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.