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
Heather_HansonHeather_Hanson 

Need help with test class...not sure why I'm getting 0 code coverage

I wrote a test class for my class related to a lightning component, but when I run it I get 0 coverage.

Here is the class that needs to be tested:
public class User_List {
@AuraEnabled
   public static list <User_List__c> fetchList(){
      
      List <User_List__c> returnUserList = new List < User_List__c > ();
        
      for(User_List__c ul: [SELECT 	id, First_Name__c, Last_Name__c, Email__c, Extension_Number__c, Type_of_User_License__c, Phone_Model__c, Language__c, Outbound_Number_Display__c, Outbound_Name_Display__c, Voicemail_Option__c, CFNR_Number__c From User_List__c LIMIT 1000]) {
             returnUserList.add(ul);
          }
         return returnUserList;
   }
}

My test class basically has me creating a record of the objects needed before a User_List record is created and then it creates a User_List record.

This is my first time working with Lightning Components and alot of my test classes do the same thing so I'm wondering if I need to do something different here?

Help would be greatly appreciated!
Best Answer chosen by Heather_Hanson
Erik YearyErik Yeary
Thanks. Basically the reason why your code coverage is 0 is because you aren't calling your method anywhere. At the end add:

List<User_List__c> testList = User_List.fetchList();

And then run some assertions on the testList to verify that it does what you want it to, like:
System.assertEquals(2, testList.size());
 

All Answers

Erik YearyErik Yeary
Can you share your test class? 
Heather_HansonHeather_Hanson
Yes, sorry, it was kind of long which is why I didn't at first.  See below.
@isTest 

public class UserListTest {
    
    static testMethod void createAccount()
    {
        //Create an account
        Account a = new Account();
        a.Name = 'Test';
        a.RecordTypeId = '0121H0000019LnWQAU';
        a.Language__c = 'ENGLISH';
        a.HPX__c = '202101';
        a.EEA__c = '202101';
        a.LOC__c = '202101';
        a.CET__c = '202101';
        a.CNS__c = '202101';
        a.CNF__c = '202101';
        a.INB__c = '202101';
        a.NET__c = '202101';
        a.Points__c = '1';
        a.Loyalty_Lead_Created__c = TRUE;
        a.Loyalty_Upselling_Report_Gatekeeper__c = TRUE;
        a.Account_Status__c = 'Active';
        a.Account_PK__c = '12345';
        a.AccountNumber = '000-000000';
        a.Service_Comments__c = 'test';
        insert a;
        System.debug('created account');
        
        //Then create a Billing contact
        Contact c = new Contact();
        c.RecordtypeId = '0121H0000019LnYQAU';
        c.FirstName = 'Paul';
        c.LastName  = 'Test';
        c.AccountId = a.id;
        c.Email = 'p@text.ca';
        c.Contact_Role__c = 'BILLING CONTACT';
        c.Employee_Status__c = 'Active';
        c.Phone = '514-722-5656';
        insert c;
        System.debug('created billing contact');
        

        //Now create an opportunity
        Opportunity o = new Opportunity();
        o.AccountId = a.id;
        o.Name = 'Test -';
        o.Service_Agreement_Type__c = 'Cisco Cloud Communications';
        o.StageName = 'NEW - CALL IN';
        o.Type = 'SALES';
        o.CloseDate = Date.today();
        o.RecordTypeId = '0121H000001EBui';
        o.Language__c = 'ENGLISH';  

        
        insert o;
        System.debug('created opportunity');
                
        //Create Programming record
        
        Programming__c prg = new Programming__c();
        
        prg.Account__c = a.id;
        prg.Opportunity__c = o.id;
        
        insert prg;
        System.debug('Programming created');
        
        //Create user list record 1
       
        User_List__c ul = new User_List__c();
        
        ul.Programming__c = prg.Id;
        ul.First_Name__c = 'Heather';
        ul.Last_Name__c = 'Hanson';
        ul.Email__c = 'h@h.ca';
        ul.Extension_Number__c = 510;
        ul.CFNR_Number__c ='555-555-5555';
        ul.Language__c = 'English';
        ul.Outbound_Name_Display__c = 'bond';
        ul.Outbound_Number_Display__c = '555-555-5555';
        ul.Phone_Model__c = '6841';
        ul.Type_of_User_License__c = 'Standard';
        ul.Voicemail_Option__c = 'No Voicemail';
        
        insert ul;
        System.debug('User List item created');
        
         //Create user list record 2
         
        User_List__c ul2 = new User_List__c();
        
        ul2.Programming__c = prg.Id;
        ul2.First_Name__c = 'Heather';
        ul2.Last_Name__c = 'Hanson';
        ul2.Email__c = 'h@h.ca';
        ul2.Extension_Number__c = 510;
        ul2.CFNR_Number__c ='555-555-5555';
        ul2.Language__c = 'English';
        ul2.Outbound_Name_Display__c = 'bond';
        ul2.Outbound_Number_Display__c = '555-555-5555';
        ul2.Phone_Model__c = '6841';
        ul2.Type_of_User_License__c = 'Standard';
        ul2.Voicemail_Option__c = 'No Voicemail';
        
        insert ul2;
        System.debug('User List item created');

    }

 
Erik YearyErik Yeary
Thanks. Basically the reason why your code coverage is 0 is because you aren't calling your method anywhere. At the end add:

List<User_List__c> testList = User_List.fetchList();

And then run some assertions on the testList to verify that it does what you want it to, like:
System.assertEquals(2, testList.size());
 
This was selected as the best answer
Heather_HansonHeather_Hanson
Ahhh that makes sense!  Thank you so much!!  100% coverage in test now!  Time to push it to production!