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
Madevala JithenderMadevala Jithender 

how di i write a testclass for a trigger

trigger AccountAddressTrigger on Account (before insert,before update) {
    
    for(Account account:Trigger.new){
        if(account.Match_Billing_Address__c==True){
            account.shippingpostalcode=account.Billingpostalcode;
        }
    }

}
Best Answer chosen by Sai Praveen (Salesforce Developers) 
mukesh guptamukesh gupta
HI Madevala,

As per Test class best bractice you need to follow salesforce best pratcice concept that's will be very usefull
 
@isTest
public class AccountAddressTriggerTest {

@testSetup static void insertAccout(){
  Account Acc = new Account(); 
  Acc.Name = 'Test Account'; 
  Acc.Match_Billing_Address__c = true; 
  insert Acc;
}

static void testMethod testAccount(){
  Account acc = [Select id, Name,Match_Billing_Address__c from Account where Name = 'Test Account'];
  system.assertEquals(true, acc.Match_Billing_Address__c );
}

}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Madevala,

Can you try the below test class it gives 100% coverage.
 
@istest
public class AccountAddressTriggerTest {
static testMethod void myTest() {
Account acc = new Account();
    acc.name='sample';
    acc.Match_Billing_Address__c=True;
    acc.Billingpostalcode='1234';
    insert acc;
    system.assertEquals('1234', acc.shippingpostalcode);
}
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
CharuDuttCharuDutt
Hii Madevala
Try Below Test Class
@isTest
public class AccountAddressTriggerTest {
	@isTest
    public static void unitTest(){
        Account Acc = new Account();
        Acc.Name = 'Test';
        Acc.Match_Billing_Address__c = true;
        insert Acc;
    }
@isTest
    public static void unitTest1(){
        Account Acc = new Account();
        Acc.Name = 'Test';
        
        insert Acc;
        Acc.Match_Billing_Address__c = true;
        update Acc;
    }
}
Please Mark It As Best Asnwer If It Helps
Thank You!
mukesh guptamukesh gupta
HI Madevala,

As per Test class best bractice you need to follow salesforce best pratcice concept that's will be very usefull
 
@isTest
public class AccountAddressTriggerTest {

@testSetup static void insertAccout(){
  Account Acc = new Account(); 
  Acc.Name = 'Test Account'; 
  Acc.Match_Billing_Address__c = true; 
  insert Acc;
}

static void testMethod testAccount(){
  Account acc = [Select id, Name,Match_Billing_Address__c from Account where Name = 'Test Account'];
  system.assertEquals(true, acc.Match_Billing_Address__c );
}

}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 
This was selected as the best answer