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
Kiru535Kiru535 

Help on test class code coverage

Please help me How to write test class for below one....

Functionality: Export to records in Excel file 

public pageReference download()
     {
     
      List<Account> dacc1 = new List<Account>();   
      accountList=null;
      MethCal=true; 
      if(accountList == null) { 
      for(Account a: [SELECT Name, Owner.Name, NameLocal, Id, Motorola_Customer_Number__c,Underserved_Account__c, AccountNumber, 
                            Assigned_MR__c, Assigned_MR__r.Name,Assigned_MR__r.id, Updated_Assigned_MR__r.id,Updated_Assigned_MR__r.Name,
                            Primary_Served__c, Secondary_Served__c,
                            Updated_Primary_Served__c,Updated_Secondary_Served__c,Updated_Underserved_Account__c
                       FROM Account 
                       WHERE Owner.Manager.Id =:selectedAsm AND Owner_Business_Group__c='Government' 
                           AND RecordTypeId =:custRecordTypeId 
                           AND (Primary_Served__c IN ('MR Led', 'MSI Led', 'Inside Sales/Tele-Sales Led') 
                           OR Secondary_Served__c IN ('MR Led', 'MSI Led', 'Inside Sales/Tele-Sales Led'))
                           Order By Name]) 
                       {                       
                           dacc1.add(a); 
                           System.debug('**********DownloadExcel**************'+dacc1.size());
                       }    
     }
     
     downloadacc = dacc1.clone();
     PageReference pageRef = new PageReference('/apex/RenderedAsExcel');
     pageRef.setRedirect(false);
     return pageRef;  
     }
pconpcon
The test for this is going to be pretty trivial.  Since all this method does is set the vaiable downloadacc and returns a page reference.  You will want to create your test account that meets your where criteria, and then call your download method.  Once you have captured that PageReference result from your download() call, you then want to compare the getUrl and the getRedirect to the expected values.  You will also probaby want to verify that the downloadacc variable was updated as expected.

If you have any specific questions about your test, please include the test code and the issue you are facing.

NOTE: Please use the "Add a code sample" button (icon <>) to add your code to make it more readable and easier to reference.