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 

@isTest help needed please

hi, i have an extention controller and a test method but i am only able to achieve 13 % converage, 3/23. This code is just queries and sending the output to a visualforce page. Can anyone please explain what i need to test for to get 100% coverage? And an example woudl be helpful too, please. Thank you!

controller:
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 from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Retail' AND Investor_Status__c = 'Active' LIMIT 1];
                channelsWholesale = [SELECT Id from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Wholesale' LIMIT 1];  
                channelsMiniCorrespondent = [SELECT Id from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Mini Correspondent' LIMIT 1];
                channelsCorrespondentAOTDirectTrade = [SELECT Id from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Correspondent AOT / Direct Trade' LIMIT 1]; 
                channelsCorrespondentBestEfforts = [SELECT Id from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Correspondent Best Efforts' LIMIT 1]; 
                channelsCorrespondentMandatoryBulkFlow = [SELECT Id from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Correspondent Mandatory (Bulk/Flow)' LIMIT 1]; 
                channelsCorrespondentNonDelegated = [SELECT Id from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Correspondent Non Delegated' LIMIT 1];
                channelsCorrespondentNonDelMandatory = [SELECT Id from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Correspondent Non-Del Mandatory' LIMIT 1];
                
                
                 
            }
        }
        
        
        
    }
    
}

test:
 
@isTest
private class extendAccountsChannel2_Test{   
    static testMethod void extendAccountsChannel2_channels(){
        
        Account aa = new Account();
        aa.name='TestInvestor';
        insert aa;
        
        //Create Retail Channel   
        Investor_Channel__c ic = new Investor_Channel__c();
        ic.Channel__c ='Retail';
        ic.Investor_Status__c = 'Active';
        ic.Account_Name__c = aa.Id;
        insert ic;
        
        //Create Wholesale Channel   
        Investor_Channel__c ic2 = new Investor_Channel__c();
        ic2.Channel__c ='Wholesale';
        ic2.Account_Name__c = aa.Id;
        insert ic2;
        
        //Create Mini Correspondent Channel   
        Investor_Channel__c ic3 = new Investor_Channel__c();
        ic3.Channel__c ='Mini Correspondent';
        ic3.Account_Name__c = aa.Id;
        insert ic3;
        
        //Create Correspondent AOT / Direct Trade   
        Investor_Channel__c ic4 = new Investor_Channel__c();
        ic4.Channel__c ='Correspondent AOT / Direct Trade';
        ic4.Account_Name__c = aa.Id;
        insert ic4;
                
           

        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);
            
         
        //Verify that records were created
   List <Investor_Channel__c> channels = [SELECT Id FROM Investor_Channel__c WHERE Account_Name__r.id = :aa.Id AND Channel__c ='Retail'LIMIT 1];
    
      
        System.assert( channels  != null);
  
    
    
    List <Investor_Channel__c> channelsWholesale = [SELECT Id FROM Investor_Channel__c  WHERE Account_Name__r.id = :aa.Id AND Channel__c ='Wholesale'LIMIT 1];
          System.assert( channelsWholesale != null);
    
    
    List <Investor_Channel__c> channelsMiniCorrespondent = [SELECT Id FROM Investor_Channel__c  WHERE Account_Name__r.id = :aa.Id AND Channel__c ='Mini Correspondent'LIMIT 1];
          System.assert( channelsMiniCorrespondent != null);
          
          List <Investor_Channel__c> channelsCorrespondentAOTDirectTrade = [SELECT Id FROM Investor_Channel__c  WHERE Account_Name__r.id = :aa.Id AND Channel__c ='Correspondent AOT / Direct Trade'LIMIT 1];
          System.assert( channelsCorrespondentAOTDirectTrade != null);
    
    
    
        
}
}

​​​​​​​
Best Answer chosen by cjen
Abhishek BansalAbhishek Bansal
Hi Cheryl,

There is a small mistake in the test class i.e. we need to set the Page Reference first and than initialize the constructor for the class, I have modified  the test class and added below:
@isTest
private class extendAccountsChannel2_Test{   
    static testMethod void extendAccountsChannel2_channels(){
        
        Account aa = new Account();
        aa.name='TestInvestor';
        insert aa;
        
        //Create Retail Channel   
        Investor_Channel__c ic = new Investor_Channel__c();
        ic.Channel__c ='Retail';
        ic.Investor_Status__c = 'Active';
        ic.Account_Name__c = aa.Id;
        insert ic;
        
        //Create Wholesale Channel   
        Investor_Channel__c ic2 = new Investor_Channel__c();
        ic2.Channel__c ='Wholesale';
        ic2.Account_Name__c = aa.Id;
        insert ic2;
        
        //Create Mini Correspondent Channel   
        Investor_Channel__c ic3 = new Investor_Channel__c();
        ic3.Channel__c ='Mini Correspondent';
        ic3.Account_Name__c = aa.Id;
        insert ic3;
        
        //Create Correspondent AOT / Direct Trade   
        Investor_Channel__c ic4 = new Investor_Channel__c();
        ic4.Channel__c ='Correspondent AOT / Direct Trade';
        ic4.Account_Name__c = aa.Id;
        insert ic4;
                
        PageReference pageRef = Page.Investor_Channel_Tabs; // Add your VF page Name here
		pageRef.getParameters().put('id', String.valueOf(aa.Id));
        Test.setCurrentPage(pageRef);

        ApexPages.StandardController sc = new ApexPages.StandardController(aa);
        extendAccountsChannel2 testAccPlan = new extendAccountsChannel2(sc);
            
         
        //Verify that records were created
   List <Investor_Channel__c> channels = [SELECT Id FROM Investor_Channel__c WHERE Account_Name__r.id = :aa.Id AND Channel__c ='Retail'LIMIT 1];
    
      
        System.assert( channels  != null);
  
    
    
    List <Investor_Channel__c> channelsWholesale = [SELECT Id FROM Investor_Channel__c  WHERE Account_Name__r.id = :aa.Id AND Channel__c ='Wholesale'LIMIT 1];
          System.assert( channelsWholesale != null);
    
    
    List <Investor_Channel__c> channelsMiniCorrespondent = [SELECT Id FROM Investor_Channel__c  WHERE Account_Name__r.id = :aa.Id AND Channel__c ='Mini Correspondent'LIMIT 1];
          System.assert( channelsMiniCorrespondent != null);
          
          List <Investor_Channel__c> channelsCorrespondentAOTDirectTrade = [SELECT Id FROM Investor_Channel__c  WHERE Account_Name__r.id = :aa.Id AND Channel__c ='Correspondent AOT / Direct Trade'LIMIT 1];
          System.assert( channelsCorrespondentAOTDirectTrade != null);
    
    
    
        
}
}
Please try now and let me know.

Thanks,
Abhishek Bansal.

All Answers

Abhishek BansalAbhishek Bansal
Hi cjen,

Can you please add one more line between the line #39 and #40 as suggested below:
PageReference pageRef = Page.Investor_Channel_Tabs; // Add your VF page Name here
//This is the new line you need to add
pageRef.getParameters().put('id', String.valueOf(aa.Id));
Test.setCurrentPage(pageRef);
This will help you to increase the coverage pf your class. Let me know if you still face any issues.

Thanks,
Abhishek Bansal.
cjencjen
Hi Abhishek, thank you for helping! i added the line and ran the test but still only have 3/23 13%. Any other ideas? thanks!
Abhishek BansalAbhishek Bansal
Hi Cheryl,

There is a small mistake in the test class i.e. we need to set the Page Reference first and than initialize the constructor for the class, I have modified  the test class and added below:
@isTest
private class extendAccountsChannel2_Test{   
    static testMethod void extendAccountsChannel2_channels(){
        
        Account aa = new Account();
        aa.name='TestInvestor';
        insert aa;
        
        //Create Retail Channel   
        Investor_Channel__c ic = new Investor_Channel__c();
        ic.Channel__c ='Retail';
        ic.Investor_Status__c = 'Active';
        ic.Account_Name__c = aa.Id;
        insert ic;
        
        //Create Wholesale Channel   
        Investor_Channel__c ic2 = new Investor_Channel__c();
        ic2.Channel__c ='Wholesale';
        ic2.Account_Name__c = aa.Id;
        insert ic2;
        
        //Create Mini Correspondent Channel   
        Investor_Channel__c ic3 = new Investor_Channel__c();
        ic3.Channel__c ='Mini Correspondent';
        ic3.Account_Name__c = aa.Id;
        insert ic3;
        
        //Create Correspondent AOT / Direct Trade   
        Investor_Channel__c ic4 = new Investor_Channel__c();
        ic4.Channel__c ='Correspondent AOT / Direct Trade';
        ic4.Account_Name__c = aa.Id;
        insert ic4;
                
        PageReference pageRef = Page.Investor_Channel_Tabs; // Add your VF page Name here
		pageRef.getParameters().put('id', String.valueOf(aa.Id));
        Test.setCurrentPage(pageRef);

        ApexPages.StandardController sc = new ApexPages.StandardController(aa);
        extendAccountsChannel2 testAccPlan = new extendAccountsChannel2(sc);
            
         
        //Verify that records were created
   List <Investor_Channel__c> channels = [SELECT Id FROM Investor_Channel__c WHERE Account_Name__r.id = :aa.Id AND Channel__c ='Retail'LIMIT 1];
    
      
        System.assert( channels  != null);
  
    
    
    List <Investor_Channel__c> channelsWholesale = [SELECT Id FROM Investor_Channel__c  WHERE Account_Name__r.id = :aa.Id AND Channel__c ='Wholesale'LIMIT 1];
          System.assert( channelsWholesale != null);
    
    
    List <Investor_Channel__c> channelsMiniCorrespondent = [SELECT Id FROM Investor_Channel__c  WHERE Account_Name__r.id = :aa.Id AND Channel__c ='Mini Correspondent'LIMIT 1];
          System.assert( channelsMiniCorrespondent != null);
          
          List <Investor_Channel__c> channelsCorrespondentAOTDirectTrade = [SELECT Id FROM Investor_Channel__c  WHERE Account_Name__r.id = :aa.Id AND Channel__c ='Correspondent AOT / Direct Trade'LIMIT 1];
          System.assert( channelsCorrespondentAOTDirectTrade != null);
    
    
    
        
}
}
Please try now and let me know.

Thanks,
Abhishek Bansal.
This was selected as the best answer
cjencjen
that worked! i now have 100% thank you!