You need to sign in to do that
Don't have an account?
Wesley H
Code Coverage showing inconsistent values, preventing deployment
We are attempting to move a simple Apex Class and Apex Test Class from Sandbox to Production, and blocked by Code Coverage Failure error. Our Code Coverage is showing 100% in Production (via Developer Console), yet we are seeing a Code Coverage Failure Error (coverage = 33%) even on "Validate" (not "Deploy") of non-apex change set. We have followed instructions in Knowledge Article 000335222 (https://help.salesforce.com/articleView?id=000335222&type=1&mode=1) carefully to reset Code Coverage numbers and are still seeing the above error on validation of non-apex change set as well as on attempt to deploy an apex-loaded change set including test class. Apex Class and Apex Test Class code below for reference, any help greatly appreciated!
And Test Class:
Public class AutoConvertLeads { @InvocableMethod public static void LeadAssign(List<Lead> LeadIds) { LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1]; List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>(); for(Lead currentlead: LeadIds){ Account[] matchedaccount = [SELECT Id, Name FROM Account WHERE Name=:currentlead.Company LIMIT 1]; if(matchedaccount.size() > 0) { Database.LeadConvert Leadconvert = new Database.LeadConvert(); Leadconvert.setLeadId(currentlead.id); Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel); Leadconvert.setDoNotCreateOpportunity(TRUE); //Remove this line if you want to create an opportunity from Lead Conversion Leadconvert.setAccountId(matchedaccount[0].id); MassLeadconvert.add(Leadconvert); } } if (!MassLeadconvert.isEmpty()) { List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert); } } }
And Test Class:
@isTest public class TestAutoConvertLeads{ static void createnewlead(){ User userToCreate = [SELECT id FROM user WHERE profile.name='System Administrator' Limit 1]; Test.startTest(); Lead leadToCreate =new Lead(); List<Lead> LeadIds= New List<Lead>(); leadToCreate.ownerid= userToCreate.id; leadToCreate.LastName ='Gupta'; leadToCreate.Company='Salesforce'; insert leadToCreate; LeadIds.add(leadToCreate); LeadStatus CLeadStatus = new LeadStatus(); Account AccountToCreate = new Account(); AccountToCreate.ownerid= userToCreate.id; AccountToCreate.Name = 'Salesforce'; insert AccountToCreate; AutoConvertLeads.LeadAssign(LeadIds); Test.stopTest(); } }
@Andrew, thanks, but already been down that road.
However...I found the issue: somehow my test class was missing a test method (line 3 of my test code above), so it was passing with 0/0 tests in sandbox (100%!) but bombing in the change set to Production. I fixed line 3 to read, "static testMethod void createnewlead(){" and I got 1/1 tests (also 100%) in sandbox and a flawless deployment to Production. Sorry that was so elementary, but thank you all for your help!
All Answers
I see a potential best practise violation in the Apex class above and have made a fix here. I have commented that code with the comment so you know where I am talking about.
Let me if this help
Bigger picture, I'm getting the Code Coverage Failure error every time I deploy or even validate a deployment of anything from Sandbox to Production. "Anything" includes a change set with a simple custom field (no Apex in sight), which produced the coverage error. And again, we have followed instructions in Knowledge Article 000335222 (https://help.salesforce.com/articleView?id=000335222&type=1&mode=1) carefully to reset Code Coverage numbers.
How can my Production code coverage be 100% from Development Console produce 33% Code Coverage errors?
Try having the production Developer Console open when you run the deployment. I believe it creates a log so you can see where the error is occurring so you can pinpoint the issue.
TL:DR
Have you tried running the existing test classes in Production to have it "reset" the coverage results for all classes?
Go to Settings > Custom Code > Apex Test Execution . Button <Select Tests...> , click the top check box for all and hit Run.
Maybe do it out of business hours but I don't think the performance hit to end users should be that bad.
The other thing to try, and this I would do out of hours, is to "Compile all classes" in the Apex Classes section of your Production environment.
My loose thought process, and I recall this from many moons ago, is that sometimes the coverage details get lost or corrupted over time and the recompile and manual re-run in the production environment help to straighten things out.
HTH
Andrew
@Andrew, thanks, but already been down that road.
However...I found the issue: somehow my test class was missing a test method (line 3 of my test code above), so it was passing with 0/0 tests in sandbox (100%!) but bombing in the change set to Production. I fixed line 3 to read, "static testMethod void createnewlead(){" and I got 1/1 tests (also 100%) in sandbox and a flawless deployment to Production. Sorry that was so elementary, but thank you all for your help!
a-ha
sometimes we don't see the trees for the forest ...or is it the other way around.
As a side note, testMethod is deprecated, so the preference is to use the @IsTest annotation on classes and methods.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_isTest.htm
Glad you got it sorted.
Regards
Andrew
You may have another test class that runs that code as well. Were you running all tests in your dev environment as well? After running the test(s), go to your AutoConvertLeads class, then in the top left click 'Code Coverage: None' and you can see what tests ran parts of your code and the percentage. My best guess is that more than one test class covered this code and that's what you were seeing.