You need to sign in to do that
Don't have an account?
Apex code test
Hello,
I am new to this and I want to test a simple class below but it is not executing my sql queries but through the visualforce page this works fine.
Please guide me what I am doing wrong.
public with sharing class FetchDocumentId
{
public String documentname{ get; set; }
public String docid{ get; set; }
public string getdocId()
{
return docid;
}
public void documentId()
{
documentname=System.currentPageReference().getParameters().get('documentname');
System.debug('doc name:'+documentname);
//documentname='Checklist_Comment_and_Feedback_Template';
try{
List<Document> lstDocument = [Select Id,Name from Document where Name =:documentname limit 1];
System.debug('!!!!!!!!!!!!!'+lstDocument);//always null
string strOrgId = UserInfo.getOrganizationId();
string strDocUrl = 'https://'+ApexPages.currentPage().getHeaders().get('Host')+ '/servlet/servlet.FileDownload?file='+lstDocument[0].Id+'&oid=' + strOrgId;
System.debug('doc url'+strDocUrl );
docid=lstDocument[0].Id;
System.debug('docid:'+docid);
}catch(Exception e){System.debug('Document not found');}
//return docid;
}
public static testmethod void testdocumentId()
{
FetchDocumentId d= new FetchDocumentId();
//This is the page that uses this controller
Test.setCurrentPageReference(new PageReference('Page.OHSESMain'));
System.currentPageReference().getParameters().put('documentname', 'Checklist_Comment_and_Feedback_Template');
Test.startTest();
d.documentId();
String docmentId=d.getdocId();
Test.stopTest();
//assert results
System.assert(docmentId!=null,'Document is null');
}
}
Thanks
Hi,
Starting from Winter 13 salesforce is not allowed to query the records from Database at the time of executing the test methods.
please insert the document record before it calls documentId method. Sample code below:
Document d1 = new Document(Name='Checklist_Comment_and_Feed_back_Template',Body=Encodingutil.base64Decode('testing Document upload'),IsPublic=true);
insert d1;
Thanks,
Srikanth. K