3

UPDATE: Sorry! I probably should have included this image to show how I would like the resulting VF page to appear. I'm new to MVC programming and the various controllers in Salesforce, so maybe a StandardSetController is not what I need. I just need a list of Call Notes to display without any title area or sidebar that normally shows. This is how it should look. This screen will be displayed in an iFrame of another app, so the other app will need to pass the relevant Case Id to this custom VF page (see the address bar URL) as a parameter in order to show the related child Call Note records:

call notes list

I'm unable to get code coverage for a Standard Set Controller. The intent is to have the controller list the call note records contained in a custom object called Call_Note__c, which is a child of the Case object. I assume I need to just have the test object navigate to the standard set controller page in order to execute the code within? I pass the Case Id to the custom VF page as a parameter, like this:

  .../apex/CustomCallNote2?id=500e0000005pLiL

Here is the StandardSetController class called listCallNotesConExt2:

public class listCallNotesConExt2 {

    public List<Call_Note__c>  callnotes {
        get {
            String idval = ApexPages.currentPage().getParameters().get('id');
            // idval = idval.substring(0, 14);
            if(callnotes == null) {
                callnotes = [SELECT id, Call_Date__c, Call_Note__c, Contact_Type__c, 
                   Contact_Description__c, CreatedById FROM Call_Note__c 
                   WHERE Case__c = :idval];
            }
            return callnotes;
        }
        set;
    }
}

Here is my VF page called CustomCallNote2. I purposely do not want to show the header or title:

<apex:page controller="listCallNotesConExt2" showHeader="false" title="false">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!callnotes}" var="cn">
            <apex:column value="{!cn.Call_Date__c}" />
            <apex:column value="{!cn.Call_Note__c}" />
            <apex:column value="{!cn.Contact_Type__c}" />
            <apex:column value="{!cn.Contact_Description__c}" />
            <apex:column value="{!cn.CreatedById}" />
        </apex:pageBlockTable>        
    </apex:pageBlock>
</apex:page>

Finally, my test class. When I execute the test class, it does not show a failure, but it also does not show that there was any code coverage (i.e. 0%):

@istest(SeeAllData=true)
private class testListCallNotesConExt2 {

    static testMethod void theTests() {
        // create a test case record
        Case c = new Case(Case_Manager__c='Joseph Bliss', Attorney__c='003e000000RjtaM', WebLeadSource__c='Internet',Status='Existing Client', Sub_Status__c='Case Updates', 
                          Plaintiff_First_Name__c='Marc', Plaintiff_Last_Name__c='Juliano');
        insert c;
        String caseId = c.Id;

        // create a test call note linked to the above Case record
        Call_Note__c cn = new Call_Note__c(Case__c=caseId, Call_Note__c='Test', Contact_Type__c='Phone Call', Contact_Description__c='Test Call Note 1');
        insert cn;

        // navigate to the page and pass the case id as a parameter
        PageReference ref = Page.CustomCallNote2; 
        ref.getParameters().put('id', caseId);
        ref.setRedirect(true);
    }
}
  • 1
    Why are you using SeeAllData=true, I might be missing something but I don't see any reason you need to be using it – EricSSH Apr 9 '15 at 22:07
  • That was a parameter that someone else told me to use when I wasn't creating the call note in the test. I think they had me doing a Select with a Limit=1 to just grab a single call note. So this was a leftover that should have been removed. Thanks. – Aurelius Apr 10 '15 at 15:03
3

You need to actually use the controller in the test. Here's an example of testing with Visualforce: http://www.eltoro.it/ArticleViewer?id=a07A000000NPRhkIAH

In your case, that would include something like:

// Set test page passing initial parameters
PageReference ref = Page.CustomCallNote2; 
ref.getParameters().put('id', caseId);
Test.setCurrentPage(ref);

// Instantiate the controller
listCallNotesConExt2 ctrl = new listCallNotesConExt2();

System.assert(1, ctrl.callnotes.size());
// add more tests here
|improve this answer|||||
0

There are a few issues with you test class.

  1. You are using SeeAllData=truewhen you seriously don't need to. You are already creating the data you need.
  2. You are not initializing your controller.
  3. You actually created a custom controller. Not an extension for a StandardSetController.

So, having stated all that, here's how you would resolve it.

First of all, to have a page use the StandardSetController, your VisualForce page would have the following:

<apex:page standardController="Call_Note__c" recordSetVar="callNotes" extensions="listCallNotesConExt2">
   ...
   <apex:pageBlockTable value="{!selected}" var="cn">

In your controller, you can then set it up like so:

private ApexPages.StandardSetController StandardSetsObjectController;
public List<Call_Note__c> SelectedCallNotes { get; private set; }

public listCallNotesConExt2(ApexPages.StandardSetController setStandardsObjectController) 
{
    StandardSetsObjectController = setStandardsObjectController;
    SelectedCallNotes = (List<Call_Note__c>)StandardsObjectController.getSelected();
}

Now to test all this, the following testMethod should do the trick:

static testMethod void SimpleTest() 
{
    Case testingCase = new Case(Case_Manager__c = 'Joseph Bliss', Attorney__c = /*Create test Attorney for Id*/, 
        WebLeadSource__c = 'Internet', Status='Existing Client', Sub_Status__c = 'Case Updates', 
        Plaintiff_First_Name__c = 'Marc', Plaintiff_Last_Name__c = 'Juliano');
    insert testingCase;

    Call_Note__c callNote = new Call_Note__c(Case__c = testingCase.Id, Call_Note__c = 'Test', 
        Contact_Type__c = 'Phone Call', Contact_Description__c = 'Test Call Note 1');
    insert callNote;

    Test.startTest();

    ApexPages.StandardSetController standardsObjectController = new ApexPages.StandardSetController();
    standardsObjectController.setSelected(new List<Call_Note__c>{ callNote });
    listCallNotesConExt2 MyExampleControllerExtension  = new listCallNotesConExt2(standardsObjectController);

    /*Perform testable actions*/

    Test.stopTest();

    /*Assert Results*/
}

I used one of my old questions here to help answer your question. Hope it helps out.

|improve this answer|||||
  • Thanks @Programmable. I updated/clarified my original question above. Would I need to do any of your suggested code changes if I only need a list, with no need for selecting items? – Aurelius Apr 10 '15 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.