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
Sunny GSunny G 

Urgent: How to write the Test class for the trigger???

Hi,

 

Please help me to write the test class for the following trigger that has more then 75% code coverage.

 

 

trigger change_opps on Pre_Sales_Request__c (before update) 
{
for(Pre_Sales_Request__c a:Trigger.new)        
{            
if(system.trigger.OldMap.get(a.Id).Opportunity__c != system.trigger.NewMap.get(a.Id).Opportunity__c)            
{                                               
a.Previous_Stage__c = a.stage__c;                     
}                                       
}
}

 

 

 

Thanks

SargeSarge

Hi Sunny,

 

   Here is how you can do it

 

1) Create  an Apex class choosing appropriate name (ChangeOppTriggerTest or something like that)

2) create a unit test method "public static testMethod void <method_name>()"

3) Create a test account record and create 2 opportunity records (associating account just created) and then create a Pre_sales_Request record associating first one of the opportunities just created. Then change the opportunity associtaed (update) to this Pre_Sales_request.

4) Test the results for new value in Previous_Stage__c field using System.assertEquals(), This should match with stage of 2nd opportunity record created.

 

This should cover your trigger code.

 

 

Sunny GSunny G

Hi Sarge,

 

I have written the following test class. But it has only 66% code coverage.. any suggestion to make it atleast 75%......

 

public class PreSalesRequest_TriggerTest{
    public static testmethod void testTrigger1() {
        Pre_Sales_Request__c psrc=new Pre_Sales_Request__c( name = 'a' );      
        insert psrc;      
        psrc.name = 'b';      
        update psrc;
        Pre_Sales_Request__c updatedName = [SELECT name FROM Pre_Sales_Request__c WHERE Id = :psrc.Id];
        System.assertEquals('b', updatedName.name);
    }
}

SargeSarge

Hi Sunny,

 

  You can go according to the steps I have mentioned in my previous post to this thread. Following is the code for it

 

 

public class PresalesRequest_TriggerTest{    
  public static testMethod void testBeforeTrigger1(){
      //Test Data initialization
      Account a  = new Account(name='Test Account 1'); //please include any required fields
      insert a;
      Opportunity opp1 = new Opportunity(name = 'Test Opp1', accountId = a.Id, stagename='First Satge', closedate=System.today()+1);
      Opportunity opp2  = new Opportunity(name='Test Opp2', accountId = a.Id,stagename = 'Second Stage'. closedate = System.today()+3);
      /*
         Include all the other required fields in both opp1 and opp2 to insert.
      */
       List<Opportunity> oppsToInsert = new List<Opportunity>();
       oppsToInsert.add(opp1);
       oppsToInsert.add(opp2);
       insert oppsToInsert;

       Pre_Sales_Request__c psr = new Pre_Sales_Request__c(name='a', Opportunity__c = opp1.Id);
       insert psr;
    
     //Making necessary changes to execute trigger
       psr.Opportunity__c = opp2.Id;

       update psr;//Now the trigger executes

       System.assertEquals(opp2.stagename, [Select Previous_Stage__c from Pre_Sales_Request__c WHERE Id = :psr.Id].Previous-Stage__c);

        //End of testmethod
 
  }
}

 

 

Sunny GSunny G

Hi Serge,

 

while run this test.. its giving the following test failure:

 

Test Failures 
Method Name
Total Time (ms)
Message
Stack Trace
PresalesRequest_TriggerTest2.testBeforeTrigger21811.0System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please enter the value: [ContactName__c]Class.PresalesRequest_TriggerTest2.testBeforeTrigger2: line 14, column 8 External entry point
SargeSarge

Hi,

 

  There must be some validation rule that prevents insert of opportunities. Please check the validation rule in Opportunities and fulfil the requirement of the validation rule.

To my guess, the ContactName__c is the lookup field to contact. If so create a test contact record before inserting opportunities and associate the contact id inserted to this field. If this field is not a lookup then fill some dummy value to proceed with unit test.

 

Again before inserting please fill all the required fields in Account or in Opportunities that will not break the system validation rules or the custom validation rules configured.

 

Hope this helps.