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
Ananthanarayanan NemmaraAnanthanarayanan Nemmara 

writing trigger test class

Hello All,

I am very new to writing test classes for triggers and it would be great if someone can help me write a test class for the below trigger. I have already tried writing the test class but not able to pass it in sandbox.

trigger CheckBeforeUpdate on Requests__c (before update) {
    
    for(Requests__c cr : Trigger.New)
    {
        String oldStatus=Trigger.oldMap.get(cr.Id).Status__c;
        if((cr.Status__c=='Approved' || cr.Status__c=='Pending Approval - Sales Mgr')  && oldStatus!='%Pending%')
        {

            if(cr.Invoice_Number__c==null || cr.Invoice_Date__c==null || cr.Expected_Payment_Date__c==null) 
            {
                cr.addError('You must fill the invoice number and invoice date in order to approve the request');
            }
        }
              
    }
      
}

Test class:

@isTest
public class CheckBeforeUpdate_TestClass{
    static testMethod void  CheckBeforeUpdateTest()
    {
        requests__c obj = new requests__c();
        obj.Account__c='001f400000B4c9NAAR';
        obj.Description__c='Testing';
        obj.Period_From__c=Date.newInstance(2016, 12, 9);
        obj.Period_To__c=Date.newInstance(2018, 12, 9);
        obj.Status__c='Pending';
        
        Test.startTest();
        try{
    
 
        obj.Status__c='Approved';
         update obj;
      
      

   } catch(DmlException error) {

     System.assertEquals(StatusCode.FIELD_CUSTOM_VALIDATION_EXCEPTION, error.getDmlType(0));
      System.assertEquals('You must fill the invoice number and invoice date in order to approve the request', error.getDmlMessage(0));
                               } 
        Test.stopTest();
    }
}

Can someone please let me know what is that I am missing. Getting thsi error "System.AssertException: Assertion Failed: Expected: FIELD_CUSTOM_VALIDATION_EXCEPTION, Actual: MISSING_ARGUMENT" and 0% code coverage

Any help would be appreciated.

Regards,
Anand
 
Best Answer chosen by Ananthanarayanan Nemmara
Abdul KhatriAbdul Khatri
Use the below test class. Please note never use hard coded values for the Id as you did for the Account__c. Fixed it in the below class.
 
@isTest
public class CheckBeforeUpdate_Test {
    
    static testMethod void  CheckBeforeUpdateTest()
    {
		Account accountNew = new Account (Name = 'Test Account');
        insert accountNew;
        
        requests__c obj = new requests__c();
        obj.Account__c = accountNew.Id;
        obj.Description__c ='Testing';
        obj.Period_From__c = Date.newInstance(2016, 12, 9);
        obj.Period_To__c = Date.newInstance(2018, 12, 9);
        obj.Status__c = 'Pending';
        insert obj;
        
        Test.startTest();
        try{
            requests__c objToUpdate = new requests__c(Id = obj.Id);
	        obj.Status__c='Approved';
    	    update obj;
        } catch(DmlException error) {
     		System.assertEquals(StatusCode.FIELD_CUSTOM_VALIDATION_EXCEPTION, error.getDmlType(0));
      		System.assertEquals('You must fill the invoice number and invoice date in order to approve the request', error.getDmlMessage(0));
        } 
        Test.stopTest();
    }
}

 

All Answers

Ananthanarayanan NemmaraAnanthanarayanan Nemmara
And the below test passes but code coverage is 0%:

@isTest
public class CheckBeforeUpdate_TestClass{
    static testMethod void  CheckBeforeUpdateTest()
    {
        Test.startTest();
        requests__c obj = new requests__c();
        obj.Account__c='001f400000B4c9NAAR';
        obj.Description__c='Testing';
        obj.Period_From__c=Date.newInstance(2016, 12, 9);
        obj.Period_To__c=Date.newInstance(2018, 12, 9);
        obj.Status__c='Pending';
        
        
        try{
    
 
        obj.Status__c='Approved';
          } catch(DmlException error) {

     System.assertEquals(StatusCode.FIELD_CUSTOM_VALIDATION_EXCEPTION, error.getDmlType(0));
      System.assertEquals('You must fill the invoice number and invoice date in order to approve the request', error.getDmlMessage(0));
                               } 
        Test.stopTest();
    }
}
 
Abdul KhatriAbdul Khatri
Use the below test class. Please note never use hard coded values for the Id as you did for the Account__c. Fixed it in the below class.
 
@isTest
public class CheckBeforeUpdate_Test {
    
    static testMethod void  CheckBeforeUpdateTest()
    {
		Account accountNew = new Account (Name = 'Test Account');
        insert accountNew;
        
        requests__c obj = new requests__c();
        obj.Account__c = accountNew.Id;
        obj.Description__c ='Testing';
        obj.Period_From__c = Date.newInstance(2016, 12, 9);
        obj.Period_To__c = Date.newInstance(2018, 12, 9);
        obj.Status__c = 'Pending';
        insert obj;
        
        Test.startTest();
        try{
            requests__c objToUpdate = new requests__c(Id = obj.Id);
	        obj.Status__c='Approved';
    	    update obj;
        } catch(DmlException error) {
     		System.assertEquals(StatusCode.FIELD_CUSTOM_VALIDATION_EXCEPTION, error.getDmlType(0));
      		System.assertEquals('You must fill the invoice number and invoice date in order to approve the request', error.getDmlMessage(0));
        } 
        Test.stopTest();
    }
}

 
This was selected as the best answer
Ananthanarayanan NemmaraAnanthanarayanan Nemmara
Hi Abdul,

Thanks a lot for youyr help. Got 100% code coverage as well as the test class is passing. But when I migrate it to production I am still getting the below error that code coverage is 0% for the trigger.
error

Do you know why I am facing this issue?
Have my other test classes passed and code coverage is above 75%
User-added image
User-added image

Regards,
Anand
Abdul KhatriAbdul Khatri
What your changeset contains?

Can you run all test in production and check the code coverage?
Amit Chaudhary 8Amit Chaudhary 8
Please include test class as well in your change set.
Abdul KhatriAbdul Khatri
Are you including Test class part of your deployment?

Is your trigger active?

Did you check how much coverage you have on Production?
Abdul KhatriAbdul Khatri
Sir what happened you are giving us details so that we can help.
Anand NemmaraAnand Nemmara
Sorry for the delay in response. Thanks a lot abdul and amit. Yes I was not adding the test class for change set and hence getting a 0% error. Now its all resolved.

Thanks for your help guys
Abdul KhatriAbdul Khatri
Can you please mark the best answer that helped you out with the test class
Abdul KhatriAbdul Khatri
Please do your part if was helpful.
Abdul KhatriAbdul Khatri
Can you please mark it resolved?
Anand NemmaraAnand Nemmara
Hi Abdul,

The reason that i haven't marked this question as solved and marked the answer as best answer is that i dont find those button here on this page and not that i don't know what my part is and don't want to do that. I had tried to do it the day this issue was resolved but couldn't find it that day and later on didn't get time to come back here. So if you could please let me know how do I mark it as resolvfed and mark the best answer I shall happily do it.

Regards,
Anand
Ananthanarayanan NemmaraAnanthanarayanan Nemmara
The reason I didn't find that option was I was logging in from a different account and seeing this question.
Thanks once again guys.
Appreciate your help.

Regards,
Anand
Abdul KhatriAbdul Khatri
Thanks.

I thought your intiial reason for this issue was the test class which you was not able to pass and not the deployment.

Anyways I am glad at least you marked it resolved.