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
michael davis 67michael davis 67 

need help writing a test class for both a trigger and helper class

Hi All,

I was able to get 75% coverage for my trigger by writing the test class below, however, the code coverage for the trigger helper class is 0, what am I doing wrong?
Here is my trigger:
trigger sessy on session__c (before insert, after insert, after update, before update, before delete) {
    if(recursive.runonce()){
       
        sess  handler = new sess();       
       
       
if(trigger.isbefore && trigger.isupdate){
     sess.onbeforeupdate(trigger.old, trigger.new,trigger.oldmap, trigger.newmap) ; 
         }   
   
   
}
}
here is my helper class:

public class sess {
    public static void onbeforeupdate(list<session__c> oldlist,list<session__c> newlist,map<ID, session__c> oldmap, map<ID, session__c> newmap){
       
    for (AggregateResult ar : [Select Count(ID) numRecs, Alex__c acctid From event__c Where Alex__c In:Trigger.New Group By Alex__c]){ Id acctId = (Id) ar.get('acctId');
    session__c acct = (session__c)Trigger.newMap.get(acctId);
    acct.mike__c = (Decimal) ar.get('numRecs');
}
          
   
    }
   
  }
and here is my tes class for the trigger -HELP PLEASE, why is the helper class gettig 0 coverage?

@isTest
public class sesstest{
static testmethod void     onbeforeupdatetest(){
 Session__c SEPT = new session__c () ;
 
 SEPT.name = 'hippy';
 SEPT.desc__c = 'rte';
 
 
 Insert SEPT;
 
 
 event__c justy = new event__c () ;
 
 justy.name = 'hoppy';
 justy.alex__c = sept.id;
 
 
 
 insert justy;
 
 SEPT.desc__c = 'rtfff';
Test.Starttest();
 Update SEPT;
sess sc = new sess();
 
Test.StopTest();
SEPT = [select ID, Mike__c from session__c where id = :sept.id];
aggregateresult jkl = [select count(ID) ctt, Alex__c tyt from event__c where Alex__c =:sept.id group by Alex__c];
System.assertequals(SEPT.MIKE__C,null);
}}

 
PawanKumarPawanKumar
Hi,
Please try with below and let me know.


trigger sessy on session__c(before insert, after insert, after update, before update, before delete) {
 if (trigger.isbefore && trigger.isupdate) {
  sess.onbeforeupdate(trigger.old, trigger.new, trigger.oldmap, trigger.newmap);
 }
}

 public class sess {
  public static void onbeforeupdate(list < session__c > oldlist, list < session__c > newlist, map < ID, session__c > oldmap, map < ID, session__c > newmap) {

    for (AggregateResult ar: [Select Count(ID) numRecs, Alex__c acctid From event__c Where Alex__c In: newlist Group By Alex__c]) {
     Id acctId = (Id) ar.get('acctId');
     session__c acct = (session__c) newmap.get(acctId);
     acct.mike__c = (Decimal) ar.get('numRecs');
    }
}    
Amit Chaudhary 8Amit Chaudhary 8
Try to update your code like below

NOTE:- look like you are using only update event

Trigger :-
trigger sessy on session__c (before update) 
{
	sess.onbeforeupdate(trigger.old, trigger.new,trigger.oldmap, trigger.newmap) ; 
}

Halper Class :-
public class sess 
{
    public static void onbeforeupdate(list<session__c> oldlist,list<session__c> newlist,map<ID, session__c> oldmap, map<ID, session__c> newmap)
	{
		for (AggregateResult ar : [Select Count(ID) numRecs, Alex__c acctid From event__c Where Alex__c In:newlist Group By Alex__c])
		{ 
			Id acctId = (Id) ar.get('acctId');
			session__c acct = (session__c)newmap.get(acctId);
			acct.mike__c = (Decimal) ar.get('numRecs');
		}
    }
}

Test Class:-
@isTest
public class sesstest
{
	static testmethod void onbeforeupdatetest()
	{
		session__c SEPT = new session__c () ;
		SEPT.name = 'hippy';
		SEPT.desc__c = 'rte';
		Insert SEPT;


		event__c justy = new event__c () ;
		justy.name = 'hoppy';
		justy.alex__c = sept.id;
		insert justy;

		SEPT.desc__c = 'rtfff';
		
		Test.Starttest();
		
			Update SEPT;
			
		Test.StopTest();
		
		SEPT = [select ID, Mike__c from session__c where id = :sept.id];
		aggregateresult jkl = [select count(ID) ctt, Alex__c tyt from event__c where Alex__c =:sept.id group by Alex__c];
		System.assertequals(SEPT.MIKE__C,null);
	}
}


Let us know if this will help you