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
tangotango 

Test coverage for Apex class on VF page to display Campaign Members

I have a VF page to display the campaign members and i used an apex class to return a campaign Memeber List into a pageblocktable.

Everything works ok but i am having difficutly with creating the test coverage.
Code:
public class CampMember {
public final Campaign camp;
public CampMember(ApexPages.StandardController controller) {
     this.camp = (Campaign)Controller.getRecord();  
    }
public List<CampaignMember> getMemberList(){
    List<CampaignMember> MemberList;
    if(MemberList == null) MemberList = 
    [select ID,CampaignId,Contact.email, ContactId,Status,FirstRespondedDate,Contact.AccountId,Contact.VAR_Reseller_ID__c from CampaignMember where CampaignId = :camp.id ];
    return MemberList;
    }
public Integer getNumberReg(){
    Integer NumberReg = 0 ;
    for(CampaignMember Reglist : [select ID,CampaignId,Contact.email, ContactId, Status from CampaignMember where CampaignId = :camp.id AND status = 'Registered' ])
        { NumberReg++;     
          }
   return NumberReg;
  }
}

this gives an error with the class constructor..
 
Code:
static testMethod void testCampMemberpage() {

PageReference pageRef = Page.CampMember;
Test.setCurrentPageReference(pageRef);
CampMember PageCon = new CampMember();

PageCon.getMemberList();
    }

 Also curious if I can calculate the reg while generating the list view to be more efficient with the code.







Sam.arjSam.arj
You are trying to test a Controller extension. Testing extensions is a bit different.
Please see the below post for more information:

http://salesforcesource.blogspot.com/2008/09/testing-your-controller-extentions.html

http://salesforcesource.blogspot.com/2008/09/testing-your-controller-extentions.html

tangotango
Thanks for the examples ! Makes its so much easier! Test coverage completed..

question... Since we are using the test object as a part of the class construction do I still need to set the current page parameters (ID? )?

Code is working correctly with real data but the test asserts are failing expected values.

Code:
static testMethod void testCampMember() {

PageReference pageRef = Page.CampMember;
Test.setCurrentPageReference(pageRef);

// Create testing Camp, Contact and Campaign member data 
Campaign tCampaign = new Campaign(name='testcamp');
insert tCampaign;

Contact tcontact = new Contact(FirstName='JimTester',LastName='testing');
insert tcontact;
CampaignMember tCampMemb = new CampaignMember(campaignId=tCampaign.id,contactId=tcontact.id,status='Registered');
insert tCampMemb;

Contact t2contact = new Contact(FirstName='sallyTester',LastName='testing');
insert t2contact;
CampaignMember t2CampMemb = new CampaignMember(campaignId=tCampaign.id,contactId=t2contact.id,status='Registered');
insert t2CampMemb;

// create the standard controller and controller extension
ApexPages.StandardController sc = new ApexPages.standardController(tcampaign);
CampMember testPageCon = new CampMember(sc);

// controller methods 
System.assertequals(2,testPageCon.getNumberReg());


.....

Class method .... public Integer getNumberReg(){ Integer NumberReg = 0 ; for(CampaignMember Reglist : [select ID,CampaignId,Contact.email, ContactId, Status from CampaignMember where CampaignId = :camp.id AND status = 'Registered' ]) { NumberReg++; } return NumberReg; }