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

Trouble increasing test coverage
I am a beginner at this, and have a fairly basic Trigger I am using. It is achieving 94% code coverage (16/17). When I try to deploy to production it fails with an error message of 0% code coverage. Not sure how to increase to at least 1%.
Below is both my Trigger code and Apex Class Test code:
TRIGGER CODE:
trigger LeadConvert on Lead (after insert) {
list<database.leadconvert> leadsToConvert = new list<database.leadconvert>();
Database.LeadConvert lc;
string convertedStatus = '';
list<leadstatus> convertStatusList = new list<leadstatus>();
convertStatusList = [Select Id, MasterLabel
from LeadStatus
where IsConverted=true limit 1];
if(convertStatusList.size() > 0){
convertedStatus = convertStatusList[0].MasterLabel;
}else{
convertedStatus = 'Closed - Converted';
}
for(Lead l : trigger.New){
if(l.LeadSource == 'Web'){
//This lead should be converted automatically to a Contact/Account
lc = new database.LeadConvert();
lc.setLeadId(l.ID);
lc.setDoNotCreateOpportunity(true);
lc.setConvertedStatus(convertedStatus);
leadsToConvert.add(lc);
}
}
list<database.leadconvertresult> lcrList = new list<database.leadconvertresult>();
lcrList = Database.convertLead(leadsToConvert);
}
APEX CLASS TEST CODE:
@IsTest
public class TestConvertLead {
static testmethod void convertLead() {
Lead l = new Lead();
l.FirstName = 'Jane';
l.LastName = 'Doe';
l.Company = 'Onassis Foundation (USA)';
l.Email = 'senvas@aol.com';
l.LeadSource = 'Web';
insert l;
}
}
Below is both my Trigger code and Apex Class Test code:
TRIGGER CODE:
trigger LeadConvert on Lead (after insert) {
list<database.leadconvert> leadsToConvert = new list<database.leadconvert>();
Database.LeadConvert lc;
string convertedStatus = '';
list<leadstatus> convertStatusList = new list<leadstatus>();
convertStatusList = [Select Id, MasterLabel
from LeadStatus
where IsConverted=true limit 1];
if(convertStatusList.size() > 0){
convertedStatus = convertStatusList[0].MasterLabel;
}else{
convertedStatus = 'Closed - Converted';
}
for(Lead l : trigger.New){
if(l.LeadSource == 'Web'){
//This lead should be converted automatically to a Contact/Account
lc = new database.LeadConvert();
lc.setLeadId(l.ID);
lc.setDoNotCreateOpportunity(true);
lc.setConvertedStatus(convertedStatus);
leadsToConvert.add(lc);
}
}
list<database.leadconvertresult> lcrList = new list<database.leadconvertresult>();
lcrList = Database.convertLead(leadsToConvert);
}
APEX CLASS TEST CODE:
@IsTest
public class TestConvertLead {
static testmethod void convertLead() {
Lead l = new Lead();
l.FirstName = 'Jane';
l.LastName = 'Doe';
l.Company = 'Onassis Foundation (USA)';
l.Email = 'senvas@aol.com';
l.LeadSource = 'Web';
insert l;
}
}
Please deploy test class and trigger together. It look like you are deploying only compoment.
Please let us know if this will help u,
Thanks
Amit Chaudhary
All Answers
Try to follow the best practice of writing a trigger and calling a class from it. Write all your code in the class. Look at the code coverage in the developer console which lines are not being covered. Put some system debug statements inside your class to check why those lines were not covered.
Also please make sure that you have added your test class to the package while deploying.
Please let us know if this helps.
Sumit
Thanks.
S
Please deploy test class and trigger together. It look like you are deploying only compoment.
Please let us know if this will help u,
Thanks
Amit Chaudhary