0

I am encountering something odd with this test class, and I either do not know how to phrase my issue, or it is just odd.

I have 2 triggers that I am checking. The first trigger is on an Opportunity. If I update any of a handful of fields, the trigger will update some corresponding fields on the account and all other open opportunities that are linked to that account. The second trigger is on the Account, if those same fields are updated on the Account, the second trigger will update all open Opportunities. I got my triggers set so that firing off 1 does not trigger the other. It is all working nicely in the Sandbox.

I am trying to write my test class so that I can promote this to my production instance, and am getting this issue.

The test class is set to insert an Account and Opportunity. Then, update the Account fields to check that the second trigger mentioned above fires. I am getting an error on my "system.assertequals(oCheck[0].Current_CBRN__c,'LION'" line near the bottom of the test.

System.AssertException: Assertion Failed: Expected: TexShield, Actual: LION

Here is my test code.

@isTest(SeeAllData=true)
public class TestAccountUpdateOpenOpp {

    public static testmethod void test() {

        Account aMain = New Account();

        aMain.Name = 'Trigger Test Account CT';
        aMain.ShippingCountryCode = 'US';
        aMain.ShippingStateCode = 'CT';
        aMain.Type = 'Lion'; 
        aMain.County__c = 'Armstrong'; 
        aMain.Current_CBRN__c = 'TexShield';
        aMain.Current_Cleaning_and_Repair__c = 'Firecare Services';
        aMain.Current_Footwear__c = 'HAIX';
        aMain.Current_Gloves__c = 'ESKA';
        aMain.Current_Helmets__c = 'Honeywell';
        aMain.Current_Turnouts__c = 'Globe';
        aMain.Current_Uniforms__c = '5.11 Tactical';

        insert aMain;

        system.assertequals(aMain.Current_CBRN__c,'TexShield');
        system.assertequals(aMain.Current_Cleaning_and_Repair__c,'Firecare Services');
        system.assertequals(aMain.Current_Footwear__c,'HAIX');
        system.assertequals(aMain.Current_Gloves__c,'ESKA');
        system.assertequals(aMain.Current_Helmets__c,'Honeywell');
        system.assertequals(aMain.Current_Turnouts__c,'Globe');
        system.assertequals(aMain.Current_Uniforms__c,'5.11 Tactical');

        list<Opportunity> oList = new list<Opportunity>();

        Opportunity o = New Opportunity();
        o.accountid = aMain.id;
        o.name = 'test';
        o.CloseDate = System.Today();
        o.StageName = 'Stage 1 - Qualify';

        insert o;
        oList.add(o);

        Opportunity o2 = New Opportunity();
        o2.accountid = aMain.id;
        o2.name = 'test';
        o2.CloseDate = System.Today();
        o2.StageName = 'Stage 1 - Qualify';

        insert o2;
        oList.add(o2);


        oList = [select id, current_CBRN__c, Current_Cleaning_and_Repair__c, Current_Footwear__c, Current_Gloves__c, Current_Helmets__c, Current_Turnouts__c, Current_Uniforms__c from Opportunity where id in :oList];

        system.assertequals(oList[0].Current_CBRN__c,'TexShield');
        system.assertequals(oList[0].Current_Cleaning_and_Repair__c,'Firecare Services');
        system.assertequals(oList[0].Current_Footwear__c,'HAIX');
        system.assertequals(oList[0].Current_Gloves__c,'ESKA');
        system.assertequals(oList[0].Current_Helmets__c,'Honeywell');
        system.assertequals(oList[0].Current_Turnouts__c,'Globe');
        system.assertequals(oList[0].Current_Uniforms__c,'5.11 Tactical');


        list<Account> aList = [select id, current_CBRN__c, Current_Cleaning_and_Repair__c, Current_Footwear__c, Current_Gloves__c, Current_Helmets__c, Current_Turnouts__c, Current_Uniforms__c from Account where id = :aMain.id];


        aList[0].Current_CBRN__c = 'LION';
        aList[0].Current_Cleaning_and_Repair__c = 'LION TotalCare';
        aList[0].Current_Footwear__c = 'LION / STC';
        aList[0].Current_Gloves__c = 'LION';
        aList[0].Current_Helmets__c = 'LION';
        aList[0].Current_Turnouts__c = 'LION';
        aList[0].Current_Uniforms__c = 'LION';

        update aList;      

        list<Account> aCheck = [select id, current_CBRN__c, Current_Cleaning_and_Repair__c, Current_Footwear__c, Current_Gloves__c, Current_Helmets__c, Current_Turnouts__c, Current_Uniforms__c from Account where id = :aMain.id];

        system.assertequals(aCheck[0].Current_CBRN__c,'LION');
        system.assertequals(aCheck[0].Current_Cleaning_and_Repair__c,'LION TotalCare');
        system.assertequals(aCheck[0].Current_Footwear__c,'LION / STC');
        system.assertequals(aCheck[0].Current_Gloves__c,'LION');
        system.assertequals(aCheck[0].Current_Helmets__c,'LION');
        system.assertequals(aCheck[0].Current_Turnouts__c,'LION');
        system.assertequals(aCheck[0].Current_Uniforms__c,'LION');


        list<Opportunity> oCheck = [select id, current_CBRN__c, Current_Cleaning_and_Repair__c, Current_Footwear__c, Current_Gloves__c, Current_Helmets__c, Current_Turnouts__c, Current_Uniforms__c from Opportunity where id = :o.id];

        system.assertequals(oCheck[0].Current_CBRN__c,'LION');
        system.assertequals(oCheck[0].Current_Cleaning_and_Repair__c,'LION TotalCare');
        system.assertequals(oCheck[0].Current_Footwear__c,'LION / STC');
        system.assertequals(oCheck[0].Current_Gloves__c,'LION');
        system.assertequals(oCheck[0].Current_Helmets__c,'LION');
        system.assertequals(oCheck[0].Current_Turnouts__c,'LION');
        system.assertequals(oCheck[0].Current_Uniforms__c,'LION');


   }
}

The error is saying that it is expecting the old value, but it is finding the new value. How can it be finding the new value and expecting the old?

I can post the specific trigger that this one is firing if needed.

Any help would be appreciated.

Thanks.

Edit I switched the assertionequals values, which is now showing me that my trigger is not processing. The trigger works for normal Sandbox Data. So, I suspect that I have an issue with my Test Class.

But, here is the trigger that I using.

trigger UpdateOpenOppCurrentGear on Account (before update) {

    if(checkRecursive.runOnce()){

        List<Opportunity> updatedOpps = new List<Opportunity>();
        List<Account> changedAccounts = new List<Account>();

        for(Account aNew : Trigger.New) {
        for(Account aOld : Trigger.Old) {
            Boolean updated = False;
            if(aNew.Current_CBRN__c != aOld.Current_CBRN__c){
                updated = True;
            }
            if(aNew.Current_Cleaning_and_Repair__c != aOld.Current_Cleaning_and_Repair__c){
                updated = True;
            }
            if(aNew.Current_Footwear__c != aOld.Current_Footwear__c){
                updated = True;
            }
            if(aNew.Current_Gloves__c != aOld.Current_Gloves__c){
                updated = True;
            }
            if(aNew.Current_Helmets__c != aOld.Current_Helmets__c){
                updated = True;
            }
            if(aNew.Current_Turnouts__c != aOld.Current_Turnouts__c){
                updated = True;
            }
            if(aNew.Current_Uniforms__c != aOld.Current_Uniforms__c){
                updated = True;
            }
            if(updated == True){
                changedAccounts.add(aNew);
            }
        }
        }

        if (changedAccounts.size() > 0){
            for(Opportunity o: [select id, accountid, current_CBRN__c, current_cleaning_and_repair__c, current_footwear__c, current_gloves__c, current_helmets__c, current_Turnouts__c, current_uniforms__c from Opportunity where isclosed = false and accountid in :changedAccounts]){
                account a = trigger.newmap.get(o.accountid);
                o.Current_CBRN__c = a.Current_CBRN__c;
                o.Current_Cleaning_and_Repair__c = a.Current_Cleaning_and_Repair__c;
                o.Current_Footwear__c = a.Current_Footwear__c;
                o.Current_Gloves__c = a.Current_Gloves__c;
                o.Current_Helmets__c = a.Current_Helmets__c;
                o.Current_Turnouts__c = a.Current_Turnouts__c;
                o.Current_Uniforms__c = a.Current_Uniforms__c;
                updatedOpps.add(o);
            }
        }

        if (updatedOpps.size() > 0){    
            update updatedOpps;
        }

    }
}

When I look at the developer console, I see that the if(aNew.Current_CBRN__c != aOld.Current_CBRN__c) line is being read, and skipped. However, my test class is updating the record according to the assertequals lines.

Does anything in the test class look off, other than the assertequals being in the wrong order? Maybe I am not actually updating the account record?

| improve this question | | | | |
  • 3
    In System.assertEquals, the first argument is reported as the "expected" and the second argument as the "actual" so it is best to code that way. Is that adding some confusion here? – Keith C Aug 27 '15 at 21:14
  • 1
    I do remember seeing that. However, I have a number of test classes that have it this way, as that is what I was first shown when I learned this. I didn't quite understand why the order mattered for an equal clause. But, it did finally click that the order determines which value is in which field for the error message. What the error means to me now is that my trigger is not firing for this test, even though it is working with real sandbox data. I will keep looking into it. Thanks. – smckitrick Aug 28 '15 at 17:56
1

I found the issue that I was having. I have some workflow rules that update the account when it is created. The test was inserting the account, the workflow rules were then updating the account. That workflow update was triggering my new update trigger. Since the runOnce check was the first thing in the trigger, the workflow update made that true. With that already true, my field updates in my test class went through the trigger, saw that it was already True, and stopped there.

The solution to this was to move the runOnce check further down the trigger. I moved it here.

    if(updated == True){
        if(checkRecursive.runOnce()){
            changedAccounts.add(aNew);
        }
    }


This made the trigger itself run with every update. But, unless any of the listed fields change, the runOnce check will never activate.

| improve this answer | | | | |
0

From the Salesforce documentation, the System.assertEquals signature is as follows

public static Void assertEquals(Object expected, Object actual, Object msg)

So technically your assert should look like this:

System.assertEquals(LION', oCheck[0].Current_CBRN__c);
| improve this answer | | | | |

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.