Skip to main content
I'm new to writing test classes, and I'm having some trouble on this one.  It simply shows the Opportunities that a contact is related via contact role.  I'm getting the error "Method does not exist or incorrect signature: [OpportunityContactRoles].getocrs()" on the test class

Apex:

public with sharing class OpportunityContactRoles{

    private Contact ocr;

    public OpportunityContactRoles(ApexPages.StandardController controller){

        this.ocr = (Contact)controller.getRecord();

        }

    

    public List<OpportunityContactRole> ocrs{

        get{

            if (ocrs==null){

                ocrs=[Select Id, Opportunity.Name, Role, IsPrimary From OpportunityContactRole WHERE ContactId=:ocr.Id ORDER BY Opportunity.Name, Role];

            }            

            return ocrs;

        }

        private set;

    }    

}

VF:

<apex:page standardcontroller="Contact" extensions="OpportunityContactRoles">

    <apex:outputPanel >

    <apex:form >

        <apex:pageBlock >

            <apex:pageBlockSection columns="1"> 

                <apex:dataTable value="{!ocrs}" var="ocr" id="ocrTable" rules="all" cellpadding="5px">

                    <apex:column width="300px" headerValue="Opportunity Name">

                        <apex:outputLink value="/{!ocr.Opportunity.Id}" target="_parent">{!ocr.Opportunity.Name}</apex:outputLink>

                    </apex:column>

               

                    <apex:column width="150px" headerValue="Role">

                        <apex:outputText value="{!ocr.Role}"/>

                    </apex:column>

                </apex:dataTable> 

            </apex:pageBlockSection>

        </apex:pageBlock> 

    </apex:form>

    </apex:outputPanel>

</apex:page>

Test Class:

@isTest

private class TestOpportunityContactRoles{

 

   static testMethod void TestOCR()

    {

        Account t_a = new Account(Name='TestClassAccount');

        insert t_a;

        Contact t_c = new Contact (Account=t_a, FirstName='Test', LastName='Contact');

        insert t_c;

        Opportunity t_o = new Opportunity (Account=t_a, Name='Test', CloseDate=Date.Today(), StageName='Prospecting');

        insert t_o;

        OpportunityContactRole t_ocr = new OpportunityContactRole (OpportunityId=t_o.Id, ContactId=t_c.Id, Role='Other', IsPrimary=TRUE);

        insert t_ocr;

        

        test.startTest();

        PageReference pageRef = Page.OppContactRoles;

        Test.setCurrentPageReference(pageRef);

               

        ApexPages.StandardController sc = new ApexPages.StandardController(t_c);

        OpportunityContactRoles t_ocrs = new OpportunityContactRoles(sc);

        

        LIST<OpportunityContactRoles> c = t_ocrs.getocrs();  //Error 

        system.assertEquals(t_ocr.Id, c[0].Id);

    }

}

 
7 answers
  1. Dec 18, 2015, 9:36 PM
    Crap, that's my fault.  It should just be

     

    List<OpportunityContactRoles> c = t_ocrs.ocrs;

     
0/9000