[SalesForce] Test Class for Campaign Member Trigger

I am writing a test class for a trigger and am having some difficulty developing a test method.

My trigger is on the CampaignMember object. When a new member is inserted AND that CampaignMember is a contact AND a checkbox called "Provider_Sales_Campaign__c on the associated campaign is true, a new lead is created and tied to the same campaign (with a new campaign member). For my test class, I have created a contact and associated with with a campaign member (and in turn associated that with a PRovider_Sales_Campaign__c. Below is what I have of the test class so-far and I have indicated where I am missing test code.

@isTest 
public class LeadGenfromCampaignMember_Test {
    static testMethod void TestCampaignMember (){

        Test.startTest();

        //Creates Contact to be linked to Campaign Member
        Contact testContact = new Contact(FirstName = 'TestContactF', LastName = 'TestContactL', Email = 'none@navinet.net');
        insert testContact;

        //Creates a new campaign memeber, associaites it with 1 provider sales campaign, and inserts
        CampaignMember newMember = 
        new CampaignMember(Campaign = [SELECT Id FROM Campaign WHERE Provider_Sales_Campaign__c = True LIMIT 1], Contact = testContact);
        insert newMember;

     //MISSING SYSTEM.ASSERT() CODE HERE!!!!!   

        Test.stopTest();

    }
}

Thanks in advance for any input.

Best Answer

Based on your comment this should work but is not all inclusive

Lead[] l = [Select ID, CampaignID From Lead];

system.assertEquals(1,l.size(),'Either no lead was created or multiple leads were created');
system.assertEquals(YOURCAMPAIGNID,l[0].CampaignID,'The Lead was not associated with the proper campaign');

This assumes you are NOT setting seealldata=true

If you are setting seealldata=true, stop, rethink your test class to create the appropriate records first.

Since I do not see a link in your test class to a lead or a link from the new lead to an object in your test class, all you have to go on for assertions in the data size being one. If you can filter the leads in the test by a known link that will help narrow the query..

To answer you last comment:

the records will be available UNLESS they are created in an async method. If the are then you need to wrap the code that fires of the code you are testing in a test.startTest() and test.stoptest(). After the stoptest you can do the query as stoptest forces the async to process

Related Topic