Search code examples
salesforceapexapex-code

Salesforce deploy Apex Class to production 0% code coverage


I am using Salesforce and I want to deploy a custom Apex Class from my sandbox. In production there is no Apex Classes and the estimated code coverage is 0% so when I try to deploy my class I get the following error

enter image description here

Is there a way to deploy my class ?

The Class I want to deploy is here:

Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {

        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        for(id currentlead: LeadIds){
                Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId(currentlead);                
                Leadconvert.setConvertedStatus('Qualified');
                MassLeadconvert.add(Leadconvert);
        }


        if (!MassLeadconvert.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
        }
    }
}

Test Class:

@isTest
Private class UnitTest_AutoConvert 
{
    Static TestMethod void AutoConvert()
    {
        // Create the Lead object
        Lead testLead = new Lead(
            FirstName='Demo 100800',
            LastName = 'Demo 100800 UnitTest',
            Status='Qualified',
            company='Lacosta'
        );
        insert  testLead;

        test.StartTest();
        List<Lead> lstOfLeadids = [ testLead.Id ]
        AutoConvertLeads.LeadAssign(lstOfLeadIds)

        test.stopTest();
    }
}

Solution

  • In order to meet the production deployment requirements you must meet the testing requirements. At a basic level, this means that you must maintain 75% line coverage between your production code and your test classes. This is at the aggregate level - so you can have some Apex classes with more or less coverage, but it must be 75% of all of code. Additionally, all Apex triggers require at least 1 line of test coverage to pass testing.

    Unfortunately, you have provided limited information in your question. It would be helpful if you could provide the code for your test class so we could determine why Salesforce is not executing your tests during deployment. My initial guess is that you have not decorated your test class correctly for Salesforce to know it is a test class.

    If you want a friendly introduction to testing, try the testing Trailhead: https://trailhead.salesforce.com/en/content/learn/modules/apex_testing

    Take a look at the documentation as Svatopluk recommended. Specifically make sure that following things are happening:

    1) The test class is marked as "@isTest"

    2) The test method within the class is marked as "@isTest" or "testMethod" in the declaration.

    3) The test class actually instantiates and runs code within your target class.

    4) Deploy the TargetClass and TestClass in the same change set - this is so Salesforce can actually execute the tests during deployment.

    Here is an example block:

    @isTest
    public class TestTargetClass{
    
        public static testMethod void TestExectuableMethod() {     
            Test.startTest();
            TargetClass instance_tc = new TargetClass();
            instance_tc.executable_method();
            Test.stopTest();
            System.assert(<some sort of test to confirm that your TargetClass operates correctly>);
        }
    }
    

    EDIT BASED ON POSTED TEST CODE:

    Your test code has a number of issues. First, it doesn't compile so I am not sure how you were able to get a passed test.

    Lets review the errors in the following block:

        test.StartTest();
        List<Lead> lstOfLeadids = [ testLead.Id ]
        AutoConvertLeads.LeadAssign(lstOfLeadIds)
        test.stopTest();
    

    The second line lstOfLeadids is of Type List of Lead but you are trying to populated it with an Id rather than a Lead. This needs to be a List of Id since AutoConvertLeads.LeadAssign takes a List of Ids as the parameter.

    Your instantiation of the lstOfLeadids is also wrong.

    You are missing two semicolons.

    Please use the following code:

        test.StartTest();
        List<Id> lstOfLeadids = new List<Id>{ testLead.Id };
        AutoConvertLeads.LeadAssign(lstOfLeadIds);
        test.stopTest();
    

    In your actual AutoConvertLeads class, you are setting the lead convesion status to "Qualified". This didn't work on my Sandbox, but maybe it will on yours. You should be querying for the MasterLabel on the LeadStatus object of an IsConverted record to get the correct value.