You need to sign in to do that
Don't have an account?
unit test 0% coverage for this trigger
Hi. I'm still new at this so please bear with me. Here is a working trigger that add products to opportunity upon logic:
trigger LeadConvert on Lead (after update) {
RecordType ClientLead = [SELECT Id FROM RecordType WHERE SobjectType = 'Lead' AND Name = 'Client' LIMIT 1];
for (Lead l: Trigger.new) {
// no bulk processing; will only run from the UI & only trigger is lead record type is Client
if (Trigger.new.size() == 1 && l.RecordTypeId == ClientLead.Id) {
if (Trigger.old[0].isConverted == false && Trigger.new[0].isConverted == true) {
// if a new opportunity was created
if (Trigger.new[0].ConvertedOpportunityId != null && Trigger.new[0].RecordTypeId=='012E0000000UMzBIAW') {
// update the converted opportunity with some text from the lead
Opportunity opp = [Select o.Id, o.Description from Opportunity o Where o.Id = :Trigger.new[0].ConvertedOpportunityId];
opp.Description = Trigger.new[0].Name;
update opp;
// add an opportunity line item - Banking Enrollment Fee
OpportunityLineItem bef = new OpportunityLineItem();
bef.OpportunityId = opp.Id;
bef.Quantity = 1;
bef.TotalPrice = 3696.00;
bef.PricebookEntryId = [Select p.Id From PricebookEntry p Where Id = '01uE0000000mc6AIAQ' And IsActive = true limit 1].Id;
insert bef;
// add an opportunity line item - Mandatory Blood Screening
OpportunityLineItem mbs = new OpportunityLineItem();
mbs.OpportunityId = opp.Id;
mbs.Quantity = 1;
mbs.TotalPrice = 1027.00;
mbs.PricebookEntryId = [Select p.Id From PricebookEntry p Where Id = '01uE0000000mc65IAA' And IsActive = true limit 1].Id;
insert mbs;
// add an opportunity line item - Yearly Banking Fee
OpportunityLineItem ybf = new OpportunityLineItem();
ybf.OpportunityId = opp.Id;
ybf.Quantity = 1;
ybf.TotalPrice = 150.00;
ybf.PricebookEntryId = [Select p.Id From PricebookEntry p Where Id = '01uE0000000mc6FIAQ' And IsActive = true limit 1].Id;
insert ybf;
}
}
}
}
}
Here is the trigger that covers 0%
@IsTest(SeeAllData=true)
Public class ConvertLead_test{
static testmethod void Unittest(){
Opportunity opp = new Opportunity(Name='Test opp',CloseDate=system.today(),StageName='Banking',RecordTypeId='012E0000000UMzBIAW');// You need to populate all the mandatory field to insert opportunity record
insert opp;
OpportunityLineItem mbs = new OpportunityLineItem ();
mbs.OpportunityId = opp.id;
mbs.Quantity = 1;
mbs.TotalPrice = 3696.00;
mbs.PricebookEntryId = [Select p.Id From PricebookEntry p Where Id = '01uE0000000mc6AIAQ' And IsActive = true limit 1].Id;
insert mbs;
}
}
Any help would be appreciated. Thank u in advance.
Your trigger is on Lead and on "after update" but in your test class I don't see that you are inserting any lead and updating it.
Insert a lead first and then update then your trigger will be included in your test class.
Thanks
Ankit Arora
Blog | Facebook | Blog Page
Agreed with Ankit
after
insert opp;
put below code ,
opp.StageName='Prospecting';//or whatever field you want to update
update opp;//This will invoke your trigger
Thank you Ankit_Arora and Vinit_Kumar
I've inserted the lead first, which brought me up to 19% coverage, but still not enough. When I look at code coverage, I see that Update and Insert code are not covered.
Any thoughts?
*Updated code below:
2 things come to mind:
1) In your trigger, you are checking trigger.old[0] and trigger.new[0] inside of your Lead loop instead of finding the l.Id in the oldmap and newmap.
2) Your trigger seems to be checking that the record type is a specific type, but your test method is not specifying a record type. Maybe its defaulting to a different type?
Daniel,
Please check the debug logs and see if you have any inserted record or not.
system.debug('***********+ opp.id);