0

I have a apex class that is a SOQL query on a custom object. The query works just fine, however, when trying to deploy to production, deployment fails because it seems apex class has zero % code coverage. No sure what I am missing in my test class?

apex class:

public with sharing class AccountSearchController {
    @AuraEnabled
    public static List<Retail_Account__c> searchAccounts( String searchTerm ) {
        List<Retail_Account__c> accounts = new List<Retail_Account__c>();
        if ( String.isNotBlank( searchTerm ) ) {
            List<List<SObject>> searchResults = [
                FIND :searchTerm
                RETURNING Retail_Account__c(
                    Id, Name,
                     Address__c,    
                     City__c,
                    State__c, Zip_Code__c


                    ORDER BY Zip_Code__c
                    LIMIT 5
                )
            ];
            accounts = searchResults[0];
        }
        return Accounts;
    }
}

Test Class:

@isTest
private class AccountSearchControllerTest {    
    @isTest
    static void accList(){

        Retail_Account__c a = new Retail_Account__c();
        a.name='ABC';
        a.city__c='houston';
        a.state__c='texas';
        a.zip_code__c='34609';
        insert a;

        Test.startTest();

        AccountSearchController s1=new AccountSearchController();
        Retail_Account__c a2=[Select Id,Name,Address__c,City__c,State__c,Zip_Code__c From Retail_Account__c Where name='ABC' LIMIT 1];

        Test.stopTest();

        System.assert(a2!=null);        
    }    
}

2 Answers 2

1

Coverage is only given to code that is executed during a unit test.

You need to go further than simply instantiating your target class, and actually call your searchAccounts() method.

You should also assert that the result of the call to that method returns the data you expect it to (I'd argue that assertions are more important than coverage).

1
  • That makes sense. When I try to add my test method here to return the results I want, which is simply the record name, the methods still shows as failed. Not sure what I am missing here. The query itself works fine and returns results. Just can't seem to get the method to pass or code coverage above 0%.
    – RMC
    Oct 7, 2019 at 13:50
1

The Unit test that you have added does not add any code coverage to the method searchAccounts in AccountSearchController.

As this method is static, instead of instantiating the AccountSearchController in your test, invoke this method with the search term as 'ABC'(the retail account name) and assert whether the method returns the expected Retail Account object list or not.

@isTest
private class AccountSearchControllerTest {

@isTest static void accList(){

    Retail_Account__c a = new Retail_Account__c();
    a.name='ABC';
    a.city__c='houston';
    a.state__c='texas';
    a.zip_code__c='34609';
    insert a;

    Test.startTest();

    List<Retail_Account__c> accs = AccountSearchController.searchAccounts('ABC');
    System.assertEquals(1, accs.size());
    System.assertEquals('ABC', accs[0].Name);       

    Retail_Account__c a2=[Select Id,Name,Address__c,City__c,State__c,Zip_Code__c From Retail_Account__c Where name='ABC' LIMIT 1];

    System.assert(a2!=null); 

    Test.stopTest();           
  }    
}

Try this and let me know for any questions.

3
  • Tried the recommendation you provided. However seems test is still failing. I am getting the following Error message: System.ListException: List index out of bounds: 0 Stack trace: Class.AccountSearchControllerTest.rcaList: line 23, column 1
    – RMC
    Oct 7, 2019 at 3:03
  • @RMC - Looks like the exception occurred at the line accounts = searchResults[0]; Please check if the SOSL query is returning results or not. Looks like you have forgotten to add 'IN ALL FIELDS' to the query. Your SOSL query should be like this FIND :searchTerm IN ALL FIELDS RETURNING ..... Or if the search term is always the Name field then replace the SOSL query with SOQL query.. Let me know if you have more questions. Oct 7, 2019 at 3:23
  • Thanks for the reply. Yes, I am getting results when the query runs. This error is slightly different from the previous which stated code coverage was 0%. I've tried a couple different variations but haven't overcome this just yet.
    – RMC
    Oct 7, 2019 at 10:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .