+ Start a Discussion

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>();   
      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,
                       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]) 
     downloadacc = dacc1.clone();
     PageReference pageRef = new PageReference('/apex/RenderedAsExcel');
     return pageRef;  
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.