You need to sign in to do that
Don't have an account?
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
Your testmethod statement:
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:
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
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';
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();
i didn't get it sorry, can u explain more or correct me the TestMethod please ?
Your testmethod statement:
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:
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
hello thank you for the reply, i tried the query earlier before you answer me, and it's working good now in test