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
MohaMoha 

TestMethod

hello i have this method : 

public string getViewEmailsReport()
{
logo = 'Reportlogo';
lstDocument = [Select Id,Name,LastModifiedById from Document where Name = :logo limit 1];
strOrgId = UserInfo.getOrganizationId();
String instance =URL.getSalesforceBaseUrl().toExternalForm();
strDocUrl = instance + '/servlet/servlet.FileDownload?file='+lstDocument[0].Id;
return strDocUrl ;
}

and i wrote a test method for it :

public string getViewEmailsReport()
{
logo = 'Reportlogo';
lstDocument = [Select Id,Name,LastModifiedById from Document where Name = :logo limit 1];
strOrgId = UserInfo.getOrganizationId();
String instance =URL.getSalesforceBaseUrl().toExternalForm();
strDocUrl = instance + '/servlet/servlet.FileDownload?file='+lstDocument[0].Id;
return strDocUrl ;
}

 

but it gives me an error : 

System.ListException: List index out of bounds: 0

 

 

anyhelp please

Best Answer chosen by Admin (Salesforce Developers) 
crop1645crop1645

Your testmethod statement:

lstDocument = [Select Id,Name,LastModifiedById from Document where Name = :logo limit 1];

 will not return any rows because in a testmethod, if running at V24.0 or higher, org data is not available to the testmethod except in limited circumstances (for example, Users are available). Thus lstDocument[0] will not exist

 

You can solve this problem in two ways:

  1. [best] - In your testmethod, insert the Document that you want your code to locate or
  2. Add the annotation isTest(SeeAllData=true) to the line before the definition of the testmethod.

I'm a strong believer in teaching you how to fish rather than delivering up the fish so I encourage you to read the APEX Developer's Guide on Testing Apex and especially looking at  the examples 

All Answers

Vinit_KumarVinit_Kumar

On which line you are getting this error message.Also,I see you have not declared your variable logo correctly(I mean there is no datatype associated to it).

 

It should be String logo = 'Reportlogo';

MohaMoha

sorry i made a mistake while publishing i've published the wrong testmethod this is the method : 

public string getViewEmailsReport()
    {    
        logo = 'Reportlogo';
       lstDocument = [Select Id,Name,LastModifiedById from Document where Name = :logo limit 1]; 
       strOrgId = UserInfo.getOrganizationId();
         String instance =URL.getSalesforceBaseUrl().toExternalForm();
         strDocUrl = instance + '/servlet/servlet.FileDownload?file='+lstDocument[0].Id;
             return strDocUrl ;
    }

 

and this is the Test Method : 

 

  static testMethod void testMethod1() {
    
        RedirecttoReport redirecttorep = new RedirecttoReport();
        String emailreport = redirecttorep.getViewEmailsReport();
        System.assertEquals(emailreport, 'http://..................');

 

and it gives me the error on the line : String emailreport = redirecttorep.getViewEmailsReport();

 

 

crop1645crop1645
  1. Does your testmethod create test Documents?  If not, and testmethod defined at V24.0 or higher, there will be no Documents to fetch
  2. A good guide on testing Apex is found in the Apex Developer's Guide under 'Testing Apex' - it explains how you should isolate your tests from org data and how to get around this when you can't (use @isTest(seeAlldata=true) annotation
MohaMoha

i didn't get it sorry, can u explain more or correct me the TestMethod please ?

crop1645crop1645

Your testmethod statement:

lstDocument = [Select Id,Name,LastModifiedById from Document where Name = :logo limit 1];

 will not return any rows because in a testmethod, if running at V24.0 or higher, org data is not available to the testmethod except in limited circumstances (for example, Users are available). Thus lstDocument[0] will not exist

 

You can solve this problem in two ways:

  1. [best] - In your testmethod, insert the Document that you want your code to locate or
  2. Add the annotation isTest(SeeAllData=true) to the line before the definition of the testmethod.

I'm a strong believer in teaching you how to fish rather than delivering up the fish so I encourage you to read the APEX Developer's Guide on Testing Apex and especially looking at  the examples 

This was selected as the best answer
MohaMoha

hello thank you for the reply, i tried the query earlier before you answer me, and it's working good now in test