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
mhittmhitt 

help with code coverage for deployment

Hi friends,

I am trying to deploy code to prod, but I am running into issues getting my test class to reference my code to provide the required coverage to deploy.  Can I please have some help?  When I run the test, it passes in Apex Test Execution, but the code coverage is still showing 0%.  It's like I'm missing something to connect the Test to the Class.

Apex Class
public class eVolveServiceRequestClass {

    public id eVolveServiceRequestId {get;set;}
    public List<ESR_Signing_Party__c> geteVolveServiceRequestComp(){
        List<ESR_Signing_Party__c> esp;
        esp = [SELECT Name__c, Phone__c, Email__c, Country__c, Time_Zone_PL__c FROM ESR_Signing_Party__c 
               WHERE eVolve_Service_Request__c =: eVolveServiceRequestId AND RecordTypeId =: '0122C00000091NsQAI'];
        return esp;
    }

}

Test Class
@isTest
private class TestEvolveServiceRequest {
    
    @isTest static void setupTestData()
    { 
        Account acc = new Account(
            Name = 'Test Account');
        insert acc;
        
        Contact con = new Contact(
            FirstName = 'Test', LastName = 'Contact', AccountId = acc.Id  );
        insert con;
        
        
        eVolve_Service_Request__c esr = new eVolve_Service_Request__c(
            Signing_Party_Type__c = 'Seller', Status__c = 'New', Contact_Name__c = con.Id, Signing_Date_Requested_Start__c = system.today()+5);
        insert esr;
        
        
        ESR_Signing_Party__c sp = new ESR_Signing_Party__c(Name__c = 'Test Party',
                                                           Phone__c = '123-456-7890', Email__c = 'test@email.com', 
                                                           Country__c = 'United States of America', State__c = 'TX',
                                                           Time_Zone_PL__c = 'Acre Time (UTC-5)', eVolve_Service_Request__c = esr.Id, RecordTypeId = '0122C00000091NsQAI');
        insert sp;
    }
    
    @isTest
    static void testcheckeVolveServiceRequestClass()
        
    {
        
        ESR_Signing_Party__c sp = [select id from ESR_Signing_Party__c where Phone__c = '123-456-7890'][0];
        
        try
        {
            insert   sp;
        }
        catch(Exception ex)
        {
            system.assert(ex.getMessage().contains('The ID is already attached to the record via GAB connector'));
        }
        
        
    }
}

Thank you in advance,
Best Answer chosen by mhitt
mhittmhitt
Here is the code I ended up using for this
 
@isTest
private class TestEvolveServiceRequest {
    
    //@isTest 
    @testSetup
    static void setupTestData()
    { 
        Account acc = new Account(
            Name = 'Test Account');
        insert acc;
        
        Contact con = new Contact(
            FirstName = 'Test', LastName = 'Contact', AccountId = acc.Id  );
        insert con;
        
        
        eVolve_Service_Request__c esr = new eVolve_Service_Request__c(
            Signing_Party_Type__c = 'Seller', Status__c = 'New', Contact_Name__c = con.Id, Signing_Date_Requested_Start__c = system.today()+5);
        insert esr;
        
        
        ESR_Signing_Party__c sp = new ESR_Signing_Party__c(Name__c = 'Test Party',
                                                           Phone__c = '123-456-7890', Email__c = 'test@email.com', 
                                                           Country__c = 'United States of America', State__c = 'TX',
                                                           Time_Zone_PL__c = 'Acre Time (UTC-5)', eVolve_Service_Request__c = esr.Id, RecordTypeId = '0122C00000091NsQAI');
        insert sp;
    }
    
    @isTest
    static void validateeVolveServiceRequestClass()
        
    {
        
        eVolveServiceRequestClass cls = new eVolveServiceRequestClass();
        cls.eVolveServiceRequestId = [Select Id from eVolve_Service_Request__c limit 1].Id;
        
        List<ESR_Signing_Party__c> esp = cls.geteVolveServiceRequestComp();
        system.assertEquals('Test Party', esp[0].Name__c);        
    }
}

 

All Answers

mhittmhitt
I changed testcheck to validate but now receive error System.ListException: List index out of bounds: 0 and not sure how to resolve.
GCW LabsGCW Labs
I don't see any reference to eVolveServiceRequestClass() in your test method. You're just inserting sp. Is eVolveServiceRequestClass() called from a trigger on the ESR_Signing_Party__c object? Otherwise, this test class doesn't seem related to your apex code.
mhittmhitt
Ahh, that's the missing link, this class is called from a component on VF email template to pull in a related list (Signing Party) to the primary object, eVolve Service Request.
AbhishekAbhishek (Salesforce Developers) 
Increase code coverage

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines

75 %

https://salesforce.stackexchange.com/questions/24165/why-is-75-code-coverage-required-in-salesforce/24167#24167

You can Increase your code coverage based on the above suggestions.


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
 
mhittmhitt
Here is the code I ended up using for this
 
@isTest
private class TestEvolveServiceRequest {
    
    //@isTest 
    @testSetup
    static void setupTestData()
    { 
        Account acc = new Account(
            Name = 'Test Account');
        insert acc;
        
        Contact con = new Contact(
            FirstName = 'Test', LastName = 'Contact', AccountId = acc.Id  );
        insert con;
        
        
        eVolve_Service_Request__c esr = new eVolve_Service_Request__c(
            Signing_Party_Type__c = 'Seller', Status__c = 'New', Contact_Name__c = con.Id, Signing_Date_Requested_Start__c = system.today()+5);
        insert esr;
        
        
        ESR_Signing_Party__c sp = new ESR_Signing_Party__c(Name__c = 'Test Party',
                                                           Phone__c = '123-456-7890', Email__c = 'test@email.com', 
                                                           Country__c = 'United States of America', State__c = 'TX',
                                                           Time_Zone_PL__c = 'Acre Time (UTC-5)', eVolve_Service_Request__c = esr.Id, RecordTypeId = '0122C00000091NsQAI');
        insert sp;
    }
    
    @isTest
    static void validateeVolveServiceRequestClass()
        
    {
        
        eVolveServiceRequestClass cls = new eVolveServiceRequestClass();
        cls.eVolveServiceRequestId = [Select Id from eVolve_Service_Request__c limit 1].Id;
        
        List<ESR_Signing_Party__c> esp = cls.geteVolveServiceRequestComp();
        system.assertEquals('Test Party', esp[0].Name__c);        
    }
}

 
This was selected as the best answer