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
An_jaAn_ja 

cannot get test past 40% coverage for controller to pull in Dashboard into Visualforce page

public with sharing class DashboardSnippetController
{
public DashBoardSnippetController()
{}

public string getDashboardHtml()
{
PageReference dbPage = new PageReference('https://cs4.salesforce.com/01ZP00000008goB');
Blob pageBlob = dbPage.getContent();
return pageBlob.toString();
}



@isTest


static void testDashboardSnippetController()
{
PageReference pageRef = Page.HomeDS;
Test.setCurrentPage(pageRef);

DashboardSnippetController controller = new DashboardSnippetController();
String nextPage = controller.getDashboardHtml();

// Verify that page fails without parameters

System.assertEquals('/apex/failure?error=noParam', nextPage);

// Add parameters to page URL

ApexPages.currentPage().getParameters().put('pageBlob', '');

// Instantiate a new controller with all parameters in the page

controller = new DashboardSnippetController();
nextPage = controller.getDashboardHtml();

// Verify that the success page displays

System.assertEquals('/apex/HomeDS', nextPage);


}

}

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

The docs aren't exactly clear about this, but clearly the test runner won't let you use the getContent method.

 

One way to handle this is to detect that you are in a test and return some fake data, something like:

 

PageReference dbPage = new PageReference('https://cs4.salesforce.com/01ZP00000008goB');
Blob pageBlob;

if (Test.isRunningTest())
{
   pageBlob=Blob.valueOf('Test Blob String');
}
else
{
   pageBlob = dbPage.getContent();
}
return pageBlob.toString();

 You won't be able to get coverage for the line that actually gets the content, but hopefully you'll get enough coverage to allow deployment.

All Answers

bob_buzzardbob_buzzard

Can you show us which lines aren't covered?  Also, do you get any errors or does the test complete successfully?

 

An_jaAn_ja

Hi Bob - thanks, trying to adapt the code from one of your suggestions

 

the test completes successfully - this is the only message I get when I use the Apex Test Execution option in SF:

 

ClassDashboardSnippetController
Method NametestDashboardSnippetController
Pass/FailSkip
Error MessageMethods defined as TestMethod do not support getContent call, test skipped
bob_buzzardbob_buzzard

The docs aren't exactly clear about this, but clearly the test runner won't let you use the getContent method.

 

One way to handle this is to detect that you are in a test and return some fake data, something like:

 

PageReference dbPage = new PageReference('https://cs4.salesforce.com/01ZP00000008goB');
Blob pageBlob;

if (Test.isRunningTest())
{
   pageBlob=Blob.valueOf('Test Blob String');
}
else
{
   pageBlob = dbPage.getContent();
}
return pageBlob.toString();

 You won't be able to get coverage for the line that actually gets the content, but hopefully you'll get enough coverage to allow deployment.

This was selected as the best answer
An_jaAn_ja

You are awesome! thanks - this worked:

 

public with sharing class DashboardSnippetController 
{
    public DashBoardSnippetController()
    {}
    
    public string getDashboardHtml()
    {
        PageReference dbPage = new PageReference('https://cs4.salesforce.com/01ZP00000008goB'); 
        Blob pageBlob; 
        
        if (Test.isRunningTest()) 
        { 
        pageBlob=Blob.valueOf('Test Blob String'); 
        } 
        else { pageBlob = dbPage.getContent(); 
        } 
        return pageBlob.toString();
    }

bob_buzzardbob_buzzard

Glad to hear you got there.