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
Jonathan SheehanJonathan Sheehan 

Problem with Test Code

My test code is only covering 36% of my Apex trigger and I'm stuck on how to get it up...Can someone please point me in the right direction?

TRIGGER:
trigger TempFieldsToCampaignAndEvent on Contact (after insert) {

        List<CampaignMember> membersToAdd = new List<CampaignMember>();
        List<Event> NewEvents = new List<Event>();
    
            for (Contact c: Trigger.new) {
                     if(c.TempCampaign__c != null) {
                        CampaignMember cm = new CampaignMember(CampaignId=c.TempCampaign__c, ContactId=c.Id, Status=c.TempCampaignStatus__c);
                        membersToAdd.add(cm);
                    }
                    system.debug(c.TempEventDescription__c);
                    if(c.TempEventDescription__c != null){
                        Event e         = new Event();
                        e.StartDateTime       = c.TempEventTime__c;
                        e.ActivityDateTime    = c.TempEventTime__c;
                        e.Subject           = c.TempEventSubject__c;
                        e.WhatId              = c.TempCampaign__c;
                        e.Description       = c.TempEventDescription__c;
                        e.WhoId             = c.Id;
                        e.OwnerId           = '0051a000000hmuN';
                        e.DurationInMinutes           = 0;
                        NewEvents.add(e);
                        }           
                    }
    database.insert (membersToAdd, false);
    insert NewEvents;    
    }

TEST CLASS
@IsTest
public class AAAllTest {

    public testMethod static void contactInsertion() {

        // Consider setting some address fields as required by copyAddressFields
        Contact c = new Contact(lastname='testing', firstname='apex');
        insert c;

        //Make some assertions based on the results of the copyAddressFields call
    }

    public testMethod static void contactInsertionFails() {

        // Set some fields that will cause an Exception in copyAddressFields
        Contact c = new Contact(lastname='testing', firstname='apex');
        insert c;

        //Make some assertions that errors were added to the Contact
    }

    public testMethod static void bulkifedInsertion() {

        Contact[] contactsToCreate = new Contact[]{};

        for(Integer x=0; x<200;x++){
            Contact ct = new Contact(lastname='testing',firstname='apex');
            contactsToCreate.add(ct);
        }
         Test.startTest();
            insert contactsToCreate;
            Test.stopTest();
    }

    public testMethod static void CampaignInsertion() {

        // Consider setting some address fields as required by copyAddressFields
        Campaign m = new Campaign(Name='Test Campaign');
        insert m;

        //Make some assertions based on the results of the copyAddressFields call
    }
 public testMethod static void EventInsertion() {

        // Consider setting some address fields as required by copyAddressFields
        date ctd = Date.today(); // will set the todays date
        Event e = new Event(Subject='Test', StartDateTime=ctd, EndDateTime=ctd, WhoId='0031700000AU6P9', DurationInMinutes=0);
        insert e;
        }

     public testMethod static void CampaignMembers() {

            // Consider setting some address fields as required by copyAddressFields
            date ctd = Date.today(); // will set the todays date
            CampaignMember cm = new CampaignMember(CampaignId='70117000001ARcU', ContactId='0031700000AU6P4', Status='Responded');
            database.insert (cm, false);



        //Make some assertions based on the results of the copyAddressFields call
    }


       
    
}
Best Answer chosen by Jonathan Sheehan
Amit Chaudhary 8Amit Chaudhary 8
Hi Jonathan Sheehan,

Please try below code.
@IsTest
public class AAAllTest 
{
   public testMethod static void CampaignInsertion() 
	{

        date ctd = Date.today(); // will set the todays date
        Event e = new Event(Subject='Test', StartDateTime=ctd, EndDateTime=ctd, WhoId='0031700000AU6P9', DurationInMinutes=0);
        insert e;

        Campaign m = new Campaign(Name='Test Campaign');
        insert m;

        Contact c = new Contact(lastname='testing', firstname='apex');
		c.TempCampaign__c = m.id;
		c.TempEventDescription__c ='Test';
        insert c;

    }
}


Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Hi Jonathan Sheehan,

Please try below code.
@IsTest
public class AAAllTest 
{
   public testMethod static void CampaignInsertion() 
	{

        date ctd = Date.today(); // will set the todays date
        Event e = new Event(Subject='Test', StartDateTime=ctd, EndDateTime=ctd, WhoId='0031700000AU6P9', DurationInMinutes=0);
        insert e;

        Campaign m = new Campaign(Name='Test Campaign');
        insert m;

        Contact c = new Contact(lastname='testing', firstname='apex');
		c.TempCampaign__c = m.id;
		c.TempEventDescription__c ='Test';
        insert c;

    }
}


Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com
This was selected as the best answer
Jayant JadhavJayant Jadhav
Hi Jonathan,

Your trigger looks good implementation as per SFDC best practices but I like to point out usage of  e.OwnerId = '0051a000000hmuN'; IF you are doing any implementation of apex code do not hard code any SFDC Id, If you do this code of line will not work in production. So plz try to avoid this.

For test coverage plz try below code, which will cover 100 %.
@IsTest
public class AAAllTest 
{
   public testMethod static void ContactInsetion() 
    {

        Campaign m = new Campaign(Name='Test Campaign');
        insert m;
        List<Contact> contList=new List<Contact>();
        for(Integer i=0;i<200;i++)
        {
            Contact c = new Contact(lastname='testing', firstname='apex',
            TempCampaign__c = m.id,TempEventDescription__c='Test' TempEventTime__c=system.Datetime );
            contList.add(c);                                  
        }    
       
       //inset all 200 records
        Test.startTest();
        insert contList;
        Test.stopTest();

       // Get inserted records 
        Integer campaignMemberCount=[select count() from campaignMember];
        Integer eventCount=[select count() from Event];
        
        // Assert statement to verify trigger result
         System.assertEquals(200,campaignMemberCount);
         System.assertEquals(200,eventCount);
    }
}



 
Jayant JadhavJayant Jadhav
@Jonathan,

Plz use TempEventTime__c=system.now() instead of TempEventTime__c=system.Datetime. 
Jayant JadhavJayant Jadhav
@Jonathan,

Let me know above code works for you, If yes, then mark this as best ans. This will help others to solve similar issues.