+ Start a Discussion
Spencer WidmanSpencer Widman 

Test Class for CampaignMember APEX Class

I am trying to finish a test class for an APEX class for use with FormAssembly and PayPal.  The original example I have is for the Opportunity Object.  My use case uses the Campaign Member.  Here is the original examples from FormAssembly :

https://help.formassembly.com/help/make-salesforce-and-paypal-work-together
 
public class IPNHandlerController {

    public PageReference myIPNupdate() {
     try{
        PageReference pageRef = ApexPages.currentPage();
        //Get the value of the 'custom' parameter from current page
        String paramCustom = pageRef.getParameters().get('custom');
        opportunity = [select Id,paid__c from Opportunity where FormAssemblyID__c = :paramCustom ];

        String content = '';
        for(String key : pageRef.getParameters().keySet()){
            //Note that there is no guarantee of order in the parameter key map.
            content += key + ' : ' + pageRef.getParameters().get(key) + '\n';
        }
        opportunity.PayPalInfo__c = content;
        opportunity.paid__c = True;
        update opportunity;

        PageReference newPage = new ApexPages.StandardController(opportunity).view();
        newPage.setRedirect(true);        

        return newPage;
     } catch (System.Exception e){
         //A failure occurred
         system.debug(e);
         return null;
     }
    }

    public Opportunity opportunity {get; set;}

    public IPNHandlerController() {
    }
}


 
@istest
private class IPNHandlerTestClass {
    public static testMethod void testMyIPNupdateSuccess(){
        IPNHandlerController ipn = new IPNHandlerController();

        Opportunity c = new Opportunity(Name='Test01',CloseDate=date.parse('1/1/2010'),StageName='Qualification',
FormAssemblyId__c='111111101111110z');
        insert c;
        ApexPages.currentPage().getParameters().put('custom', '111111101111110z');

        PageReference p = ipn.myIPNupdate();
        System.assertNotEquals(null,p);
    }

    public static testMethod void testMyIPNupdateFailure(){
        IPNHandlerController ipn = new IPNHandlerController();
        ApexPages.currentPage().getParameters().put('custom', '111111101111111z');
        PageReference p = ipn.myIPNupdate();
        System.assertEquals(null,p);
    }

    public static testMethod void testIPNHandlerController(){
        //You should customize this to fit your needs.
        System.assertEquals(true,true);
    }

}
and here is what I attempted to mimic for the CampaignMember Object.  Everything works fine in my Sandbox but when I attempt to deploy to Production I am getting a failure in my Test Class. 
 
public class IPNHandlerController {

    public PageReference myIPNupdate() {
     try{
        PageReference pageRef = ApexPages.currentPage();
        //Get the value of the 'custom' parameter from current page
        String paramCustom = pageRef.getParameters().get('custom');
        CampaignMember = [select Id,paid__c from CampaignMember where FormAssemblyID__c = :paramCustom ];

        String content = '';
        for(String key : pageRef.getParameters().keySet()){
            //Note that there is no guarantee of order in the parameter key map.
            content += key + ' : ' + pageRef.getParameters().get(key) + '\n';
        }
        CampaignMember.PayPalInfo__c = content;
        CampaignMember.paid__c = True;
        update CampaignMember;

        PageReference newPage = new ApexPages.StandardController(CampaignMember).view();
        newPage.setRedirect(true);

        return newPage;
     } catch (System.Exception e){
         //A failure occurred
         system.debug(e);
         return null;
     }
    }

    public CampaignMember CampaignMember {get; set;}

    public IPNHandlerController() {
    }
}

@istest
private class IPNHandlerTestClass {
    public static testMethod void testMyIPNupdateSuccess(){
        IPNHandlerController ipn = new IPNHandlerController();

        CampaignMember m1 = new CampaignMember (ContactId='1', CampaignId='1', Status='Sent');

        ApexPages.currentPage().getParameters().put('custom', '111111101111110z');

        PageReference p = ipn.myIPNupdate();
        System.assertNotEquals(null,p);
    }

    public static testMethod void testMyIPNupdateFailure(){
        IPNHandlerController ipn = new IPNHandlerController();
        ApexPages.currentPage().getParameters().put('custom', '111111101111111z');
        PageReference p = ipn.myIPNupdate();
        System.assertEquals(null,p);
    }

    public static testMethod void testIPNHandlerController(){
        //You should customize this to fit your needs.
        System.assertEquals(true,true);
    }

}
I am assuming it is a very simple mistake on my part due to the fact that this is new ground for me.  Can anyone help me out with a test class for a PayPal IPN Handler?

 
PriyaPriya (Salesforce Developers) 

Hey Spencer,

What is the error you are getting while deploying the classes?

Code coverage or any specific error?

Thanks & Regards,

Priya Ranjan