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
thunksalotthunksalot 

Test failing even when Trigger works perfectly when Test steps are performed manually?

I'm about to pull my hair out, so would really appreciate it if someone could help me see the simple mistake I must be making here.  I have a trigger that works perfectly when I perform manually the same steps I have written into the unit test.  I thought maybe it was that my trigger is a before update trigger, so I added some extraneous updates after the update in this test, but that didn't help.  And, when I do the updates manually exactly as in this unit test, it works, without extraneous follow-on updates.

 

Here's the trigger:

 

 

trigger searchZipCode on Account (before update) {

    // For each Account in the Trigger.new list of Accounts
    for (Account a : Trigger.new) {
        // check if the BillingPostalCode field is contained in the list of zips
        try {
            if (a.BillingPostalCode != NULL){
                if (a.ZipSearchList__c.contains(a.BillingPostalCode.substring(0,5)))
                {
                    a.ZipFound__c = TRUE;
                } else a.ZipFound__c = FALSE;
            }
        } catch (System.NullPointerException e) {
            system.debug(e.getMessage());
            a.ZipFound__c = FALSE;        
        } finally {
            // do nothing     
        }           
 }
}

 

 

 

Here's the unit test:

 

 

@isTest
private class searchZipCodeTest {

    static testMethod void testsearchZipCode() {
        String ziplist = '12345, 67890';
        String successZip = '12345';
        String failZip = '66666';
        
        Account successAccount = new Account(
            Name = 'Success University',
            // RecordType = '012A00000004OUY',
            Type = 'Graduate Institution',
            BillingPostalCode = successZip,
            ZipFound__c = FALSE
        );
        
        Account failAccount = new Account(
            Name= 'Fail University',
            // RecordType = '012A00000004OUY',
            Type = 'Graduate Institution',
            BillingPostalCode = failZip,
            ZipFound__c = FALSE
         );
        insert new Account[] { successAccount, failAccount };
 
        successAccount.ZipSearchList__c = ziplist;
        failAccount.ZipSearchList__c = ziplist;
        update successAccount;
        update failAccount;
        
        Test.StartTest();
        System.assertEquals('12345', successAccount.BillingPostalCode);
        System.assertEquals('12345, 67890', successAccount.ZipSearchList__c);
// This is the assert that fails:
System.assertEquals(TRUE, successAccount.ZipFound__c); System.assertEquals(FALSE, failAccount.ZipFound__c); Test.StopTest(); } }

 

 

 

Here's the error message:

 

Best Answer chosen by Admin (Salesforce Developers) 
EMHDevEMHDev

I've hit this problem a few times.  Before you do the system.asserts, you need to query the data to get the updated fields back.  It feels intuitive that your update calls will have updated the properties, but they have not, they have just updated the database.  So, if you put in a SOQL query to get your accounts back from the database, your asserts will work.

 

Erica

All Answers

EMHDevEMHDev

I've hit this problem a few times.  Before you do the system.asserts, you need to query the data to get the updated fields back.  It feels intuitive that your update calls will have updated the properties, but they have not, they have just updated the database.  So, if you put in a SOQL query to get your accounts back from the database, your asserts will work.

 

Erica

This was selected as the best answer
skodisanaskodisana

Hi,

 

For the successAccount you are assigning ZIP_Found__c to FALSE and in the System.assertEquals for Successaccount you are checking TRUE.

Because of this System.assertEquals id faining.

For the successAccount assign ZIP_Found__c to TRUE and Run the test class.

 

Below i have highlighted two statements.

 

        Account successAccount = new Account(
Name = 'Success University',
// RecordType = '012A00000004OUY',
Type = 'Graduate Institution',
BillingPostalCode = successZip,
ZipFound__c = FALSE
);

        System.assertEquals(TRUE, successAccount.ZipFound__c);
System.assertEquals(FALSE, failAccount.ZipFound__c);

EMHDevEMHDev

The trigger changes the zipfound value, so  I don't think that is the issue.  I am fairly sure it is a matter of requerying the data after doing the update.

thunksalotthunksalot

Thank you both for your kind attention to my pesky little problem!  I really appreciate it.

 

As m62Dev pointed out, the trigger's function is to switch the ZipFound__c field to true in the success test case (and leave it false in the fail case).  I'm going to try m62Dev's solution. 

 

Thanks again.

 

-Sam

TCDTCD

Could you please put latest test code here...

Imran MohammedImran Mohammed

If you are looking for something as below for test memthods, then this should be the thing you have to implement.

 

What you should make sure is whenever you perform a DML in test methods, you have top query the details back for doing assertion.

For ex:

Test__c t = new Test__c(name='sample', field1__c = 'field1');

insert t;

Now for assertion it will be good you query back the details like below,

Test__c[] tList = [select id, name, field1__c from Test__c where id = :t.id];

system.assertEquals(tList.size(), 1);

system.assertequals(tList[0].name, t.name);

TCDTCD

 

Thanks Imran.