+ Start a Discussion
SattibabuSattibabu 

How to write test method for the getContent() Method.

How to write test method for the below method:
public String getCaseURL()
     {
      return (new pagereference('/apex/MyVisualforcepage?id='+camp.id).getContent().toString());
     }
When i try to call this method in my test class. It's giving the following error. 
"Methods defined as TestMethod do not support getContent call, test skipped"

How to cover getContent() method in test class. Anyhelp would be greatly appreciated. 

 
Best Answer chosen by Sattibabu
SattibabuSattibabu
I got answer to this query : we can put getContent() method in try block, then test case will not throw the Exception

All Answers

NagaNaga (Salesforce Developers) 
Hi Sattibabu,

User-added imageBest Regards
Naga Kiran
SattibabuSattibabu
Thanks your response Naga !!  However i can't able to implement your workaround. I remember that i tried similar kind of solution earlier, but no luck. Can you simply the solution and tell me how can write in my context as my method is returing string.
Vivek DeshmaneVivek Deshmane
Hi Santtibabu,
Try below solution and let me know if it helps you and mark the best answer.

public String getCaseURL()
     {
      String body;
      try {
         body=new pagereference('/apex/MyVisualforcepage?id='+camp.id).getContent().toString();
      }catch (VisualforceException e) {
      body = Blob.valueOf('Some Text');
       }
      return  body;
    }

Best Regards,
-Vivek
Vivek DeshmaneVivek Deshmane
Please try this.I have test and coverd 100% and makr best answer.
public String getCaseURL()
     {
      String body;
      try {
         body=new pagereference('/apex/TestVbd').getContent().toString();
      }catch (VisualforceException e) {
      body = String.valueOf('Some Text');
       }
      return  body;
    }
Best Regards,
-Vivek
SattibabuSattibabu
I got answer to this query : we can put getContent() method in try block, then test case will not throw the Exception
This was selected as the best answer