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
JuulJuul 

postalcode testcase

Hi,

 

can someone help me in creating a good test class.....

 

this is my working trigger:

 

trigger Postalcodelookup on Account (before insert, before update) {
    List<String> Postalcodes4dig = new List<String>(); 
    
    for (Account a:Trigger.new){
        Postalcodes4dig.add(a.Postalcode_4dig__c);
    }

    List <Postalcode__c> PostalCodeList = [Select ID, Name from Postalcode__c where Name in :Postalcodes4dig];

    for (Integer i = 0; i <Trigger.new.size(); i++){
        if (PostalCodeList.size() > 0 && Trigger.new[i].Postalcode_4dig__c !=null){
                        for (Postalcode__c z:PostalCodeList){
                if (Trigger.new[i].Postalcode_4dig__c == z.name){
                        Trigger.new[i].PostalcodeLookup__c = z.ID;
                                }
                        }
        }
        else{
        Trigger.new[i].PostalcodeLookup__c = null;
        }
        
    }
}

 

 

And I start my test with the following:

 

@isTest
// test for the 'Postalcodelookup'trigger
private class Postalcodelookup {
static testMethod void test() {

        account[] Accounttest = new account[]{
         new account(Name='TEST', Shippingstreet = 'Street 10', Shippingpostalcode = '1100 AA', Shippingcity = 'Stad', Shippingcountry = 'Nederland', Phone = '077-5401111',Type = 'Prospect', Potential_amount__c = 1000000,Industry = 'Hospitality', Sub_Industry__c = 'Cultuur', Business_Unit__c = 'Zuid', Account_stage__c = 'Suspect'),
         new account(Name='TEST', Shippingstreet = 'Street 10', Shippingpostalcode = '1000 CD', Shippingcity = 'Stad', Shippingcountry = 'Nederland', Phone = '077-5401111',Type = 'Prospect', Potential_amount__c = 1000000,Industry = 'Hospitality', Sub_Industry__c = 'Cultuur', Business_Unit__c = 'Zuid', Account_stage__c = 'Suspect')
         };

 

But I don't know how to make it complete with system.assert etc....

 

Can someone please help :)

 

thanks 

Jerun JoseJerun Jose

Use system.assert methods to verify the output of your trigger.

 

Say for example.

 

There is one line where you make an update

 

Trigger.new[i].PostalcodeLookup__c = z.ID;

 

After you insert test accounts in your test method, query for that same account again, and do a

system.assertEquals(expectedValue, testrecordvalue)

 

This way, you can make sure that when others make additional updates to the system, they will be alerted through test failures if their changes are going to break your logic.