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
DannyTKDannyTK 

Test coverage on Test Class

Good afternoon,

I'm having an issue with getting proper coverage on test class (i'm not a developer).  The trigger is to bring down a field value to a custom field on an event (event field 'Fortune_1000_Ranking__c') from the related Contact record on the event.  The field originates from the Account and is a formula field on the Contact record ('Fortune_1000_2013_Ranking__c').  the trigger seems to work fine:

trigger UpdateEventFortune1000 on Event (before insert, before update) {
Set<Id>CIds = new Set<Id>();for (Event E:trigger.new)
{String wId = e.WhoId; If(wID!=null && wId.startsWith('003')&& !cIds.contains(e.WhoId))
{cIds.add(e.WhoId);}}
    
    List<Contact> EventC = [Select Id, Fortune_1000_2013_Ranking__c from Contact where
                            Id in :cIds];
    
    Map<Id, Contact> CMap = new Map<Id, Contact>(); for (Contact c: EventC)
        
    {CMap.put(c.Id,c);}
    
    for (Event e:trigger.new)
    {String wId = e.WhoId; if (wId!=null && wId.startswith('003'))
    {Contact thisC = cMap.get(e.WhoId);
    if(thisC!=null)
    {e.Fortune_1000_Ranking__c = thisC.Fortune_1000_2013_Ranking__c;}}}
 }

Yet i'm having issues with the Test class and getting proper coverage:
@isTest
private class UpdateEventFortune1000testclass {
    
static testMethod void UpdateEventFortune1000 () {
 
   
List<Account>Account = new List<Account> {new Account (
    name = 'test company',
    Fortune_1000_2013_Ranking__c = 1,
    phone = '(555) 555-5555') };
    insert Account;

List<Contact>Contact = new List<Contact> {new Contact (
    firstname = 'test',
    lastname = 'testtest',
    phone = '(555) 555-5555') };    
    insert Contact;
    
List<Event> e = new List<Event> {new Event(
    WhoID = Contact[0].id,
    subject = 'test trigger',
    Type = 'Meeting')};
    
    insert e;
    
List<Event> Eventstoupdate = New List<event>{[select id from event where id in :e]};
    for (event eok:eventstoupdate);
    
    update eventstoupdate;
    
Contact[] c = [select id,Fortune_1000_2013_Ranking__c from Contact where id in :Contact];
    System.assertEquals(1,Contact[0].Fortune_1000_2013_Ranking__c);
    
}
}

Can someone take a look at the test class and see what is wrong with it, i'm not experienced enough on the development side to really see where the error lies.

-d
Best Answer chosen by DannyTK
Bhanu MaheshBhanu Mahesh
Hi Danny,

Don't use salesforce keywords or object Names as variable names.

When inserting contact you are not associating any account to that contact.

While asserting you have to assert with event field not with contact field.

Try the below code
@isTest
private class UpdateEventFortune1000testclass {
    static testMethod void UpdateEventFortune1000 () {
		List<Account> AccountLst = new List<Account>();
		Account acc = new Account(name = 'test company',Fortune_1000_2013_Ranking__c = 1,phone = '(555) 555-5555');
		AccountLst.add(acc);
		Account acc1 = new Account(name = 'test company',Fortune_1000_2013_Ranking__c = 2,phone = '(555) 555-5555');
		AccountLst.add(acc1);
		insert AccountLst;
		List<Contact> ContactLst = new List<Contact> ();    
		Contact con = new Contact(firstname = 'test',AccountId = AccountLst[0].Id, lastname = 'testtest',phone = '(555) 555-5555');
		ContactLst.add(con);
		Contact con1 = new Contact(firstname = 'test',AccountId = AccountLst[1].Id, lastname = 'testtest',phone = '(555) 555-5555');
		ContactLst.add(con1);
		insert ContactLst;
		List<Event> e = new List<Event> {new Event(WhoID = ContactLst[0].id,subject = 'test trigger',Type = 'Meeting')};
		insert e;
		Event evnt = [SELECT Id,Fortune_1000_Ranking__c FROM Event WHERE id = :e[0].Id];
		System.assertEquals(1,evnt.Fortune_1000_2013_Ranking__c);
		evnt.WhoID = ContactLst[1].id;
		update evnt;
		Event evnt1 = [SELECT Id,Fortune_1000_Ranking__c FROM Event WHERE id = :e[0].Id];
		System.assertEquals(2,evnt.Fortune_1000_2013_Ranking__c);
	}
}

Regards,
Bhanu Mahesh

All Answers

Bhanu MaheshBhanu Mahesh
Hi Danny,

Don't use salesforce keywords or object Names as variable names.

When inserting contact you are not associating any account to that contact.

While asserting you have to assert with event field not with contact field.

Try the below code
@isTest
private class UpdateEventFortune1000testclass {
    static testMethod void UpdateEventFortune1000 () {
		List<Account> AccountLst = new List<Account>();
		Account acc = new Account(name = 'test company',Fortune_1000_2013_Ranking__c = 1,phone = '(555) 555-5555');
		AccountLst.add(acc);
		Account acc1 = new Account(name = 'test company',Fortune_1000_2013_Ranking__c = 2,phone = '(555) 555-5555');
		AccountLst.add(acc1);
		insert AccountLst;
		List<Contact> ContactLst = new List<Contact> ();    
		Contact con = new Contact(firstname = 'test',AccountId = AccountLst[0].Id, lastname = 'testtest',phone = '(555) 555-5555');
		ContactLst.add(con);
		Contact con1 = new Contact(firstname = 'test',AccountId = AccountLst[1].Id, lastname = 'testtest',phone = '(555) 555-5555');
		ContactLst.add(con1);
		insert ContactLst;
		List<Event> e = new List<Event> {new Event(WhoID = ContactLst[0].id,subject = 'test trigger',Type = 'Meeting')};
		insert e;
		Event evnt = [SELECT Id,Fortune_1000_Ranking__c FROM Event WHERE id = :e[0].Id];
		System.assertEquals(1,evnt.Fortune_1000_2013_Ranking__c);
		evnt.WhoID = ContactLst[1].id;
		update evnt;
		Event evnt1 = [SELECT Id,Fortune_1000_Ranking__c FROM Event WHERE id = :e[0].Id];
		System.assertEquals(2,evnt.Fortune_1000_2013_Ranking__c);
	}
}

Regards,
Bhanu Mahesh
This was selected as the best answer
DannyTKDannyTK
Thanks Bhanu,

one issue i'm having on validation is a System.AssertException: Assertion Failed: Expected: 2, Actual: 1 on line 23 .... would I need two inserts for events? (i'm not a developer so this may come across wrong)
Bhanu MaheshBhanu Mahesh
HI Danny,

W need to assert with evnt1
Change that line to
System.assertEquals(2,evnt1.Fortune_1000_2013_Ranking__c);

Regards,
Bhanu Mahesh
Mark this thread as 'SOLVED' if your issue is resolved.
DannyTKDannyTK
Thanks Bhanu, well deserving answer tag!