You need to sign in to do that
Don't have an account?
Ananthanarayanan 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
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
All Answers
@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();
}
}
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.
Do you know why I am facing this issue?
Have my other test classes passed and code coverage is above 75%
Regards,
Anand
Can you run all test in production and check the code coverage?
Is your trigger active?
Did you check how much coverage you have on Production?
Thanks for your help guys
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
Thanks once again guys.
Appreciate your help.
Regards,
Anand
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.