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
Stacy SennottStacy Sennott 

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;
    }
}
Best Answer chosen by Stacy Sennott
Amit Chaudhary 8Amit Chaudhary 8
Hi Stacy Sennott,

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

ssssssssssssss
Hi,

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
Amit Chaudhary 8Amit Chaudhary 8
Please check you have any validation rule in production on lead object ?. Please share screen shot
Stacy SennottStacy Sennott
No validation rule in production on Lead object. Here are my screen shots - thx!
Stacy SennottStacy Sennott
User-added imageUser-added imageUser-added image
ssssssssssssss
Did you add the test class to the change set while deploying?

Thanks.
S
Stacy SennottStacy Sennott
No did not do that - will do thank you!
Amit Chaudhary 8Amit Chaudhary 8
Hi Stacy Sennott,

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
This was selected as the best answer
Stacy SennottStacy Sennott
Succeeded! Thank you both!!!
ssssssssssssss
Please mark the reply which helped you..as the best answer so that others could benefit!