You need to sign in to do that
Don't have an account?

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
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.
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);
}
}
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
Hi Serge,
while run this test.. its giving the following test failure:
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.