0

I have tried this multiple ways with no success. I have a controller class that I need to write a test class for. I have written the test class, and see that the test method passes. However, when I deploy to production, deployment fails due to code coverage is at 0%.

Any suggestions on how I can achieve the coverage I need? What am I missing and why won't what I have here give me the proper coverage?

Controller

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
public class AccountSearchControllerTest5{
    static testMethod void TestMethod1(){
    Retail_Account__c rta1 = new Retail_Account__c(name = 'ABC',address__c = 'main st', city__c = 'Tampa', state__c ='FL', zip_code__c = '34609');
        test.startTest();
        insert rta1;
        Retail_Account__c rta11 = [select name, id, address__c, city__c, state__c, zip_code__c from Retail_Account__c where name = 'ABC'];  
        System.assertEquals('ABC', rta11.Name);
        System.assertNotEquals('Me', rta11.Name);

        Retail_Account__c rta33 = [select name, id, address__c, city__c, state__c, zip_code__c from Retail_Account__c where name = 'ABC'];
        test.stopTest();

        System.assertEquals('ABC', rta33.Name);
        System.assertNotEquals('Me', rta33.Name); 

        }
    }

Thanks in advance for the help.

2
  • Are you including the test class in your deployment?
    – Derek F
    Oct 8, 2019 at 22:07
  • Yes. Test class is selected in deployment. Still shows 0% coverage as if none of my test data is executed. I get the same results if I deploy via changeset, VS Code, or Gearset.
    – Ron
    Oct 8, 2019 at 22:10

1 Answer 1

0

You are not even calling your searchAccounts method, you should include a method invocation sentence within your test

@isTest
public class AccountSearchControllerTest5{
    static testMethod void TestMethod1(){
        Retail_Account__c rta1 = new Retail_Account__c(name = 'ABC',address__c = 'main st', city__c = 'Tampa', state__c ='FL', zip_code__c = '34609');
        test.startTest();
        insert rta1;
        Retail_Account__c rta11 = [select name, id, address__c, city__c, state__c, zip_code__c from Retail_Account__c where name = 'ABC'];  

        list<Retail_Account__c> result = AccountSearchController.searchAccounts('ABC');
        system.assertEquals(result.size(), 0);
        test.stopTest();
        }
    }
5
  • Thanks for the response. When I follow this example and run a test in the console, I get method failed.
    – Ron
    Oct 8, 2019 at 22:30
  • Did the assertion fail? Or something else in your code? Can you post the stack trace?
    – Ideasforce
    Oct 8, 2019 at 22:34
  • Here is the error and stack trace: Error message: System.AssertException: Assertion Failed: Expected: 0, Actual: 1 Stack trace: Class.AccountSearchControllerTest5.TestMethod1: line 10, column 1
    – Ron
    Oct 8, 2019 at 22:42
  • I forgot SOSL queries return an empty set of search results within test methods (see for reference: developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…). To make the test pass, just put a 0 instead of a 1 —I already edited my original response.
    – Ideasforce
    Oct 8, 2019 at 22:53
  • Thats a great reference document. I appreciate the help. Didn't realized I wasn't actually calling the method. Thanks a million.
    – Ron
    Oct 8, 2019 at 23:32

You must log in to answer this question.

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