+ Start a Discussion
Eric Blaxton 11Eric Blaxton 11 

Issues with a simple SOQL Query test class...no coverage at all.

Hi and thanks in advance for your help.  I wrote this test and can't figure out why I can't get any coverage.  Seems so simple.

public with sharing class AccountStatus
{

 @AuraEnabled

    public static Account getAccount(Id accountId)

    {

        //Query fields from Parent record to prepopulate New RTO

        return [SELECT Id, Name, Account_Status__c, Bill_To_DUNs_Number__c,  

                BillingCountry,BillingStreet,BillingCity,BillingState,BillingPostalCode,

                Billing_County__c,BillingStateCode,Legal_Entity_Name__c,Type__c,Account_Record_ID__c,

                Current_Brand__c

                FROM Account where id = :accountId];    

    }

}


Test class

@isTest
private class AccountStatusTest

{

   

    static testMethod void getAccount()

    {

        Account a = new Account();

        a.name='Test';

        a.phone='12345';        

        a.Account_Status__c = 'Active Account';

        a.Bill_To_DUNs_Number__c = '1234567890';

        a.BillingCountry = 'United States';

        a.BillingStreet = 'Main Street';

        a.BillingCity = 'Dallas';

        a.BillingState = 'Texas';

        a.BillingPostalCode = '76543';

        a.Billing_County__c = 'Blackland';

        a.BillingStateCode = 'TX';

        a.Legal_Entity_Name__c = 'TestParent';

        a.Type__c = 'Retail Outlet';

       // a.Account_Record_ID__c = '0120W000001USkRQAW';

        a.Current_Brand__c = 'Sunoco';

        insert a;

       

        Account a2=[SELECT Id, Name, Account_Status__c, Bill_To_DUNs_Number__c,  

                BillingCountry,BillingStreet,BillingCity,BillingState,BillingPostalCode,

                Billing_County__c,BillingStateCode,Legal_Entity_Name__c,Type__c,Account_Record_ID__c,

                Current_Brand__c

                FROM Account where id = :a.id];

        System.assert(a2!=null);        

    }    

}

Thank You,
Eric

 
Best Answer chosen by Eric Blaxton 11
Arvind_SinghArvind_Singh
Hello Eric,
Test class need to call main class and execute method of class by passing record Id explicitly in order to cover code. here is some sample code 
@isTest
private class AccountStatusTest

{
    
    @IsTest
    static void getAccount()
        
    {
        
        Account a = new Account();
        
        a.name='Test';
        
        a.phone='12345';        
        
        insert a;
        
        Account Ac = AccountStatus.getAccount(a.Id);  // Add this method and it should cover 100%
        System.assert(ac!=null);        
        
    }    
    
}

Hope it helps. 

All Answers

David Zhu 🔥David Zhu 🔥
You may replace:

 Account a2=[SELECT Id, Name, Account_Status__c, Bill_To_DUNs_Number__c,  

                BillingCountry,BillingStreet,BillingCity,BillingState,BillingPostalCode,

                Billing_County__c,BillingStateCode,Legal_Entity_Name__c,Type__c,Account_Record_ID__c,

                Current_Brand__c

                FROM Account where id = :a.id];


with the following code:

 Account a2 = AccountStatus.getAccount(a.Id);
 
Eric Blaxton 11Eric Blaxton 11
Hi David,

Your suggestion cleaned it up, but the code coverage for this class is still at 0%.

Eric
David Zhu 🔥David Zhu 🔥
Have you saved the change and run again?
Eric Blaxton 11Eric Blaxton 11
Yes, ran it twice.  Let me review again.
 
jacob freyjacob frey
What's the filter on the SOQL that fails? My guess is you're filtering by something that won't match nay of the inserted test records.
Arvind_SinghArvind_Singh
Hello Eric,
Test class need to call main class and execute method of class by passing record Id explicitly in order to cover code. here is some sample code 
@isTest
private class AccountStatusTest

{
    
    @IsTest
    static void getAccount()
        
    {
        
        Account a = new Account();
        
        a.name='Test';
        
        a.phone='12345';        
        
        insert a;
        
        Account Ac = AccountStatus.getAccount(a.Id);  // Add this method and it should cover 100%
        System.assert(ac!=null);        
        
    }    
    
}

Hope it helps. 
This was selected as the best answer