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
Anna Antonella Adiletta 1Anna Antonella Adiletta 1 

Test Class help for my apex code

I have to write test class for this apex code:

public static void CreaEventoCorrispondente() {


        if (trigger.isInsert) {

            Map<Integer, Event> EventstoUpsert1 = new Map<Integer, Event> ();
            Map<Integer, Event> EventstoUpsert2 = new Map<Integer, Event> ();
            List<Schema.PicklistEntry> ple = Edizione__c.Lead_Tutor__c.getDescribe().getPicklistValues();
            List<String> cognomiTutor = new List<String> ();
            for (Schema.PicklistEntry p : ple) {
                cognomiTutor.add(p.getLabel());
            }
            Map<String, Id> CognomiIds = new Map<String, Id> ();
            for (User u :[SELECT Id, LastName FROM User WHERE LastName IN :cognomiTutor]) {
                CognomiIds.put(u.LastName, u.Id);
            }

            integer pos = 0;

        
            for (Edizione__c e : (List<Edizione__c>) trigger.new) {

                if (e.Lead_Tutor__c != NULL && CognomiIds.keySet().contains(e.Lead_Tutor__c)) {
                    Event newEvent1 = new Event(isAllDayEvent = TRUE, Location=e.Sede__c, Subject = e.Name, StartDateTime = (DateTime.newInstance(e.Data_inizio__c , (DateTime.newInstance(e.Data_inizio__c, Time.newInstance(0,0,0,0))).timeGMT())), EndDateTime = (DateTime.newInstance(e.Data_fine__c, (DateTime.newInstance(e.Data_fine__c, Time.newInstance(0,0,0,0))).timeGMT())), OwnerId = CognomiIds.get(e.Lead_Tutor__c));
                    EventstoUpsert1.put(pos, newEvent1);
                }
                if (e.Lead_Tutor__c != NULL && CognomiIds.keySet().contains(e.Second_Tutor__c)) {
                    Event newEvent2 = new Event(isAllDayEvent = TRUE, Location=e.Sede__c, Subject = e.Name, StartDateTime = (DateTime.newInstance(e.Data_inizio__c, (DateTime.newInstance(e.Data_inizio__c, Time.newInstance(0,0,0,0))).timeGMT())), EndDateTime = (DateTime.newInstance(e.Data_fine__c, (DateTime.newInstance(e.Data_fine__c, Time.newInstance(0,0,0,0))).timeGMT())), OwnerId = CognomiIds.get(e.Second_Tutor__c));
                    EventstoUpsert2.put(pos, newEvent2);
                }
                pos++;
            }
            if (!EventstoUpsert1.isEmpty()) {
                insert EventstoUpsert1.values();
                for (Integer i : EventstoUpsert1.keySet()) {
                    Edizione__c ed = ((Edizione__c) trigger.new[i]);
                    ed.IdEventoLeadTutor__c = EventstoUpsert1.get(i).Id;
                }

            }
            if (!EventstoUpsert2.isEmpty()) {
                insert EventstoUpsert2.values();
                for (Integer i : EventstoUpsert2.keySet()) {
                    Edizione__c ed = ((Edizione__c) trigger.new[i]);
                    ed.IdEventoSecondTutor__c = EventstoUpsert2.get(i).Id;
                }

            }

        } else if (trigger.isUpdate) {

            Map<Id, Event> EventstoUpsert1 = new Map<Id, Event> ();
            Map<Id, Event> EventstoUpsert2 = new Map<Id, Event> ();
            List<Schema.PicklistEntry> ple = Edizione__c.Lead_Tutor__c.getDescribe().getPicklistValues();
            List<String> cognomiTutor = new List<String> ();
            List<Id> EventstoDelete = new List<Id>();
            for (Schema.PicklistEntry p : ple) {
                cognomiTutor.add(p.getLabel());
            }
            Map<String, Id> CognomiIds = new Map<String, Id> ();
            for (User u :[SELECT Id, LastName FROM User WHERE LastName IN :cognomiTutor]) {
                CognomiIds.put(u.LastName, u.Id);
            }

            for (Edizione__c e : (List<Edizione__c>) trigger.new) {
                if (e.Lead_Tutor__c != ((Edizione__c) trigger.oldMap.get(e.Id)).Lead_Tutor__c) {
                    if (((Edizione__c) trigger.oldMap.get(e.Id)).Lead_Tutor__c == NULL){
                        Event ev = new Event();
                        ev = new Event(isAllDayEvent = TRUE, Subject = e.Name, Location=e.Sede__c, StartDateTime = (DateTime.newInstance(e.Data_inizio__c, (DateTime.newInstance(e.Data_inizio__c, Time.newInstance(0,0,0,0))).timeGMT())), EndDateTime = (DateTime.newInstance(e.Data_fine__c, (DateTime.newInstance(e.Data_fine__c, Time.newInstance(0,0,0,0))).timeGMT())), OwnerId = CognomiIds.get(e.Lead_Tutor__c));
                        EventstoUpsert1.put(e.Id, ev);
                    } else if (e.Lead_tutor__c == NULL) {
                        EventstoDelete.add(e.IdEventoLeadTutor__c);
                        e.IdEventoLeadTutor__c = NULL;
                    } else {
                        if (((Edizione__c) trigger.oldMap.get(e.Id)).IdEventoLeadTutor__c != NULL) {
                            EventstoUpsert1.put(e.Id, new Event(Id = ((Edizione__c) trigger.oldMap.get(e.Id)).IdEventoLeadTutor__c, OwnerId = CognomiIds.get(e.Lead_Tutor__c)));
                        }
                    }
                }
                if (e.Second_Tutor__c != ((Edizione__c) trigger.oldMap.get(e.Id)).Second_Tutor__c) {
                    if (((Edizione__c) trigger.oldMap.get(e.Id)).Second_Tutor__c == NULL){
                        Event ev = new Event();
                        ev = new Event(isAllDayEvent = TRUE, Subject = e.Name, Location=e.Sede__c, StartDateTime = (DateTime.newInstance(e.Data_inizio__c, (DateTime.newInstance(e.Data_inizio__c, Time.newInstance(0,0,0,0))).timeGMT())), EndDateTime = (DateTime.newInstance(e.Data_fine__c, (DateTime.newInstance(e.Data_fine__c, Time.newInstance(0,0,0,0))).timeGMT())), OwnerId = CognomiIds.get(e.Second_Tutor__c));
                        EventstoUpsert2.put(e.Id, ev);
                    } else if (e.Second_tutor__c == NULL) {
                        EventstoDelete.add(e.IdEventoSecondTutor__c);
                        e.IdEventoSecondTutor__c = NULL;
                    } else {
                        if (((Edizione__c) trigger.oldMap.get(e.Id)).IdEventoSecondTutor__c != NULL) {
                            EventstoUpsert2.put(e.Id, new Event(Id = ((Edizione__c) trigger.oldMap.get(e.Id)).IdEventoSecondTutor__c, OwnerId =  CognomiIds.get(e.Second_Tutor__c)));
                        }
                    }
                }
            }

            if (!EventstoUpsert1.isEmpty()) {
                upsert EventstoUpsert1.values();
                for (Id i : EventstoUpsert1.keySet()) {
                    Edizione__c ed = ((Edizione__c) trigger.newMap.get(i));
                    ed.IdEventoSecondTutor__c = EventstoUpsert1.get(i).Id;
                }
            }
            if (!EventstoUpsert2.isEmpty()) {
                upsert EventstoUpsert2.values();
                for (Id i : EventstoUpsert2.keySet()) {
                    Edizione__c ed = ((Edizione__c) trigger.newMap.get(i));
                    ed.IdEventoSecondTutor__c = EventstoUpsert2.get(i).Id;
                }
            }
            if (!EventstoDelete.isEmpty()){
                List<Event> ETDL = new List<Event>();
                for (Id i : EventstoDelete){
                    ETDL.add(new Event(Id = i));
                }
                delete ETDL;
            }

        }

    }

This class retrive an apex trigger :

trigger Trigger_Edizione on Edizione__c (before update, before insert, after update, before delete) {
if (trigger.isBefore){
        if (!trigger.isDelete){
            EdizioneHelper.CreaEventoCorrispondente();
        } else {
            EdizioneHelper.CancellaEventoCorrispondente();
        }
    } 




The test class that I have writed is :

static testMethod void TestCreaEventoCorrispondente() {
     
     Map<Integer, Event> Events1 = new Map<Integer, Event> ();
      Map<Integer, Event> Events2 = new Map<Integer, Event> ();
     List <Schema.PicklistEntry> l_picklist = new List <Schema.PicklistEntry>();
     List <Id>l_Ids= new List <Id>();
     Map<String,Id> S_Ids = new Map <String,Id> ();
       
List <Edizione__c> l_ed= new List <Edizione__c>();
     Edizione__c edizione = new Edizione__c(Data_inizio__c=date.valueof('2016-09-07'),Data_fine__c=date.valueof('2016-09-07'),motivo_stato__c='edizione confermata', Codice_piattaforma__c='edizione',Sede__c='Milano',Name='ciccio',Data_Invio_al_Sito__c=date.valueof('2016-09-15'),Lead_Tutor__c='Colonna');
        insert edizione;
     l_ed.add(edizione);
   
       
        List <Event> l_ev = new List <Event>(); 
        Event ev = new Event (Location=edizione.Sede__c,Subject=edizione.name, IsAllDayEvent=false, StartDateTime = datetime.newInstance(2014, 11, 13, 16, 30, 0), EndDateTime = datetime.newInstance(2014, 11, 13, 18, 30, 0));
         l_ev.add(ev); 
        insert ev;
        
      
         User us = new User (Username='d.pippo@bridgepartners.it.dev',LastName='Pugliese',FirstName='Domenico', Email='d.pugliese@bridgepartners.it', Alias='dpugl', CommunityNickname='d.pippo', TimeZoneSidKey='Europe/Rome', LocaleSidKey='it_IT', EmailEncodingKey='ISO-8859-1', ProfileId='00e58000000ZU0WAAW', LanguageLocaleKey='it');
        insert us;
         update us;
    
     EdizioneHelper.CreaEventoCorrispondente();
     
     update ev;
     EdizioneHelper.CreaEventoCorrispondente();
     
     delete ev;
     EdizioneHelper.CreaEventoCorrispondente();
     
     delete l_ev;
     EdizioneHelper.CreaEventoCorrispondente();
   
         
     ev.IsAllDayEvent=true;
     update ev;
     EdizioneHelper.CreaEventoCorrispondente();
     
     edizione.Second_Tutor__c='Fioretti';
     update edizione;
     EdizioneHelper.CreaEventoCorrispondente();
     
 }
   

The result of run test  is :


System.NullPointerException: Attempt to de-reference a null object, when I write in the test class
EdizioneHelper.CreaEventoCorrispondente(); 
and when I write   if (trigger.isInsert)  in the apex class


 
MoggyMoggy
you can not call the EdizoneHelper.CreaEventoCorrispondente() directly in your test class
As you have references to a trigger this is defineltely a trigger handler class, therefore your test code should simply create a situation
that this responsible trigger is fired,

take out the direct calls "EdizoneHelper.CreaEventoCorrispondente()"  and see what happens
create for each "event" its own test method ( Insert ; update ; delete ) including the test data creation