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
Salesforce Test 5Salesforce Test 5 

How to write Test class for this trigger

Trigger addtrigger on Quote__c(before insert, before update)
{
  if(Trigger.isBefore) 
  {
    if (Trigger.isInsert || Trigger.isUpdate)
     {
       for(Quote__c cob : trigger.new)
         {
            cob.Total_Job_Value_Updated__c= cob.H_Total_Job_Value__c+cob.Delivery_Charges_n__c; 
           }
       }
    }
}

 
Prem Anandh 1Prem Anandh 1
Hi, 
Hope below code will help you for unit test class
@isTest
private class Quote_test {

	private static testMethod void Quote_Test() {
	    
	    
	//It will run on Before Insert Trigger
	Quote__c objQuote = new Quote__c();
        objQuote.H_Total_Job_Value__c = 10;
        objQuote.Delivery_Charges_n__c = 100;
        insert objQuote;
        
        //It will run on Before Update Trigger
        objQuote.H_Total_Job_Value__c = 20;
        update objQuote;

	}

}

 
DebasisDebasis
Hi Prem,

please check this below code which is testing bulk record scenation with asert statements.
@isTest
private class Quote_test {

	private static testMethod void Quote_Test() {
		list<Quote__c> quoteLIst = new list<Quote__c>();
		//bulkification testing
	    for(integer i = 0;i<5;i++){
			Quote__c quote = new Quote__c();
			quote.H_Total_Job_Value__c=i*10;
			quote.Delivery_Charges_n__c=i*20;
			//assign any other required field of your quote object here
			quoteLIst.add(quote);
		}
	    if(quoteLIst.size()>0)
		{
			insert quoteLIst;
		}
		//fetch inserted record and assert it to check.
		list<Quote__c> fetchQuote = [select id,Delivery_Charges_n__c,H_Total_Job_Value__c,Total_Job_Value_Updated__c from quote__c];
		system.assertEquals(fetchQuote[0].Total_Job_Value_Updated__c,fetchQuote[0].H_Total_Job_Value__c+fetchQuote[0].Delivery_Charges_n__c);
		fetchQuote[0].Delivery_Charges_n__c=0;
		update fetchQuote;
		Quote__c fetchQuote1 = [select id,Delivery_Charges_n__c,H_Total_Job_Value__c,Total_Job_Value_Updated__c from quote__c where id=:fetchQuote[0].id];
		system.assertEquals(fetchQuote1.Total_Job_Value_Updated__c,fetchQuote1.H_Total_Job_Value__c);
	}

}

 
Prem Anandh 1Prem Anandh 1
Hi Debasis, 

I have checked your code and updated few things on Assert and DML operation 
 
@isTest
private class Quote_test {

	private static testMethod void Quote_Test() {
		
		list<Quote__c> quoteLIst = new list<Quote__c>();
		
		//bulkification testing
	    for(integer i = 0;i<5;i++){
			Quote__c quote = new Quote__c();
			quote.H_Total_Job_Value__c=i*10;
			quote.Delivery_Charges_n__c=i*20;
			//assign any other required field of your quote object here
			quoteLIst.add(quote);
		}
	    
	    //If condition is not required, If quoteLIst is empty salesforce won't perform DML operations.
	    insert quoteLIst;
		
		
		//fetch inserted record and assert it to check.
		list<Quote__c> fetchQuote = [select id,Delivery_Charges_n__c,H_Total_Job_Value__c,Total_Job_Value_Updated__c from quote__c];

		//Updated Assert -> Assert should be checked from input
		system.assertEquals(fetchQuote[0].Total_Job_Value_Updated__c,
		quoteLIst[0].H_Total_Job_Value__c+quoteLIst[0].Delivery_Charges_n__c);

		//Updating Quote
		fetchQuote[0].Delivery_Charges_n__c=0;
		update fetchQuote;
		
		Quote__c fetchQuote1 = [select id,Delivery_Charges_n__c,H_Total_Job_Value__c,Total_Job_Value_Updated__c from quote__c where id=:fetchQuote[0].id];
		
		//Here input is first query line # 20, fetchQuote1 is output.
		system.assertEquals(fetchQuote1.Total_Job_Value_Updated__c,fetchQuote[0].H_Total_Job_Value__c+fetchQuote[0].Delivery_Charges_n__c);
	}

}