Search code examples
salesforceapexsalesforce-lightning

Apex Test Class - Simple Insert


Trying to teach myself Salesforce Development.

I created a Lightning Component for a Communities page to create a Lead. The component works fine, simply calls the Apex class to insert the Lead.

However, I can't figure out how to write the test class required before this can be passed to production.

Here is the Apex Class to create the Lead:

public class LightningLeadCreatecls {
    @AuraEnabled
    public static void createLead(Lead leadObj){
        insert leadObj; 
    }
}

I have written Apex Test Classes for Apex Classes that simply pull data with SELECT, but can't figure out how to create this test class. It is at 0/2.

Thanks.


Solution

  • No different as the example found in Salesforce's official site, try

    @isTest 
    private class LightningLeadCreateTest {
    static testMethod void doTest() {
       // Insert Lead
       Lead l = new Lead(LastName='Test', Company='Test', Status='Open - Not Contacted');
       LightningLeadCreatecls.createLead(l);
    
       // Retrieve the Lead
       Lead verifyLead = [SELECT LastName FROM Lead LIMIT 1];
    
       // Test that Lead exist
       System.assertEquals('Test', verifyLead.LastName);
    }
    

    }