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
cjencjen 

Need some help with @isTest - need coverage for queries only

hi, i have an apex class that just runs querries. I am trying to write the test class but failing to get any coverage. First of all, i am not too sure what i am testing here, just creating an account and a custom object related record, then query for it. My current test passes, but my controller still shows 0/23 code coverage? Any help much appreciated, thanks!
controller:

public class extendAccountsChannel2 { 
    public List <Investor_Channel__c> channels{get; set;} 
    public List <Investor_Channel__c> channelsWholesale{get; set;} 
    public List <Investor_Channel__c> channelsMiniCorrespondent{get; set;} 
    public List <Investor_Channel__c> channelsCorrespondentAOTDirectTrade{get; set;} 
    public List <Investor_Channel__c> channelsCorrespondentBestEfforts{get; set;} 
    public List <Investor_Channel__c> channelsCorrespondentMandatoryBulkFlow{get; set;}
    public List <Investor_Channel__c> channelsCorrespondentNonDelegated{get; set;} 
    public List <Investor_Channel__c> channelsCorrespondentNonDelMandatory{get; set;}  
     
    
    public Account accounts {get;set;} 
    
    public Account acc {get;set;}
    
    
    
    
    
    
    
    
    public extendAccountsChannel2(ApexPages.StandardController controller) {
        Id id = ApexPages.currentPage().getParameters().get('id');
        
        
        if(ApexPages.currentPage().getParameters().get('id') != null){
            
           id accRecId = [select id from Account where id = :id].id;
           
           
            if(accRecId != null){
                channels = [SELECT Id,name from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Retail' AND Investor_Status__c = 'Active' LIMIT 1];
                channelsWholesale = [SELECT Id,name from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Wholesale' LIMIT 1];  
                channelsMiniCorrespondent = [SELECT Id,name from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Mini Correspondent' LIMIT 1];
                channelsCorrespondentAOTDirectTrade = [SELECT Id,name from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Correspondent AOT / Direct Trade' LIMIT 1]; 
                channelsCorrespondentBestEfforts = [SELECT Id,name from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Correspondent Best Efforts' LIMIT 1]; 
                channelsCorrespondentMandatoryBulkFlow = [SELECT Id,name from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Correspondent Mandatory (Bulk/Flow)' LIMIT 1]; 
                channelsCorrespondentNonDelegated = [SELECT Id,name from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Correspondent Non Delegated' LIMIT 1];
                channelsCorrespondentNonDelMandatory = [SELECT Id,name from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Correspondent Non-Del Mandatory' LIMIT 1];
                
                
                 
            }
        }
        
        
        
    }
    
}
 
@isTest
private class extendAccountsChannel2_Test{   
    static testMethod void extendAccountsChannel2_channels(){
        
        Account aa = new Account();
        aa.name='TestInvestor';
        insert aa;
        
           
        Investor_Channel__c ic = new Investor_Channel__c();
        ic.Channel__c ='Retail';
        ic.Account_Name__c = aa.Id;
        insert ic;
        
        //Verify that records were created
    for(Investor_Channel__c channels : [SELECT Id FROM Investor_Channel__c WHERE Account_Name__r.id = :aa.Id AND Channel__c ='Retail'LIMIT 1]){
          
    }    
}
}
Christan G 4Christan G 4
Hi CJen, I hope you are well. The reason why your test coverage states 0/23 is because there is no connection between your controller class and your test class. 

I found an article that I think will help you resolve your issue. Please refer to this link: How to Test StandardController Classes (https://salesforce.stackexchange.com/questions/102740/how-to-write-test-class-for-standard-controller-along-with-extensions)

If you need additional help, feel free to reach out and I'll help you create a test code for this.
cjencjen
Hi, i was able to connect the controller class and the test class and noew getting 3/23 code coverage. I do need some more help as i am not understanding exaclty what i am testing for. i am created an Account and some related records, then querying for those records. What do i need to do to get 100% coverage? thanks! 
cjencjen
i added this, get 3/23
ApexPages.StandardController sc = new ApexPages.StandardController(aa);
        extendAccountsChannel2 testAccPlan = new extendAccountsChannel2(sc);
         
        PageReference pageRef = Page.Investor_Channel_Tabs; // Add your VF page Name here
        Test.setCurrentPage(pageRef);
cjencjen
@christian G 4 i am not sure how to mention you here, lol