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
subodh chaturvedi 17subodh chaturvedi 17 

My test class is not giving any error & running successfully but while checking code coverage it is giving none

I mentioned my class with respective test class. please let me know where I am doing wrong.

My class is calling in a trigger which fires when my support Request(object) Record is approved by the approver through the standard approval process. when the record is approved the trigger automatically create an Event record. below is the class with test class .

Public class AutoCreateEvent
{
    Public static void createNewEvent(List<DealSupportRequest__c> dsreq, Map<Id, DealSupportRequest__c> oldDsreqMap)
    {
        
        List<Event> EventRec = new List<Event>();
        RecordType SuppReqRecordType = [SELECT Id
                                         FROM RecordType
                                         WHERE SobjectType = 'DealSupportRequest__c' AND DeveloperName = 'PSCUOnsiteSupport'
                                         LIMIT 1];
        for(DealSupportRequest__c ds:dsreq)
        {
            
            if((ds.Is_approved__c==true && ds.RecordtypeId==SuppReqRecordType.Id) && (ds.Is_approved__c !=          oldDsreqMap.get(ds.Id).Is_approved__c) )
            {
               Event e  = new Event();
                e.WhatId = ds.Account__c;
                e.Type   = 'On-Site at PSCU';
                e.Status__c = 'Scheduled';    
                e.OwnerId = ds.Ownerid;
                e.Subject_Custom__c =ds.Purpose__c; 
                e.Description = ds.OtherPurpose__c;
                e.StartDateTime =ds.StartDateTime__c;
                e.EndDateTime = ds.EndDateTime__c;
                e.LocationCallInInfo__c = ds.CampusLocation__c;
                e.Support_Request__c = ds.Id;
                EventRec.add(e);
              
          }
      }
        If(EventRec.size()>0)
        Insert EventRec;
    }
}

Test class:
@IsTest
public class AutoCreateEvent_Test {

    static testmethod void  CreateEvent(){
      Test.startTest();
        
            User u = new User(
            ProfileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id,
            LastName = 'last',
            Email = 'puser000@amamama.com',
            Username = 'puser000@amamama.com' + System.currentTimeMillis(),
            CompanyName = 'TEST',
            Title = 'title',
            Alias = 'alias',
            TimeZoneSidKey = 'America/Los_Angeles',
            EmailEncodingKey = 'UTF-8',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_US',
            EmployeeNumber='1'
        );
        insert u;
        List<Event> EventRec = new List<Event>();
    
        Account a = new Account(Name='1st Community CU',Membership_Defined__c='Member',Type='Client', ownerid=u.id );
        Insert a;
    
        DealSupportRequest__c dsr = new      DealSupportRequest__c(Account__c=a.Id,StartDateTime__c=system.now(),EndDateTime__c=system.now().addDays(1),ownerid=a.Ownerid);
        Insert dsr;
        
         Event e = new Event();
            e.WhatId=a.Id;
            e.Type   = 'On-Site at PSCU';
            e.Status__c = 'Scheduled';
            e.OwnerId = dsr.Ownerid;
          //  e.Support_Request__c=dsr.Id;
            e.StartDateTime =dsr.StartDateTime__c;
            e.EndDateTime = dsr.EndDateTime__c;
            EventRec.add(e);
        
        insert EventRec;
    Test.stopTest() ;   
    }
}
Best Answer chosen by subodh chaturvedi 17
Steven NsubugaSteven Nsubuga
@IsTest
public class AutoCreateEvent_Test {

    static testmethod void  CreateEvent(){
      Test.startTest();
        
            User u = new User(
            ProfileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id,
            LastName = 'last',
            Email = 'puser000@amamama.com',
            Username = 'puser000@amamama.com' + System.currentTimeMillis(),
            CompanyName = 'TEST',
            Title = 'title',
            Alias = 'alias',
            TimeZoneSidKey = 'America/Los_Angeles',
            EmailEncodingKey = 'UTF-8',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_US',
            EmployeeNumber='1'
        );
        insert u;
        List<Event> EventRec = new List<Event>();
    
        Account a = new Account(Name='1st Community CU',Membership_Defined__c='Member',Type='Client', ownerid=u.id );
        Insert a;
    
        DealSupportRequest__c dsr = new      DealSupportRequest__c(Account__c=a.Id,StartDateTime__c=system.now(),EndDateTime__c=system.now().addDays(1),ownerid=a.Ownerid);
        Insert dsr;

        List<DealSupportRequest__c> dsreq = new List<DealSupportRequest__c>();
        dsreq.add(dsr);

        Map<Id, DealSupportRequest__c> oldDsreqMap = new Map<Id, DealSupportRequest__c>();
        oldDsreqMap.put(dsr.Id, dsr);

        AutoCreateEvent.createNewEvent(dsreq, oldDsreqMap);
         
    Test.stopTest() ;   
    }
}

 

All Answers

Steven NsubugaSteven Nsubuga
There is no reference to the AutoCreateEvent class and its method createNewEvent in your test class AutoCreateEvent_Test
subodh chaturvedi 17subodh chaturvedi 17
Hi Steven,
Can you please let me know how to give a reference & where 
Steven NsubugaSteven Nsubuga
You need to make use of the class in your test class. That is what I mean. 
In your AutoCreateEvent class, you have a method called createNewEvent. You need to call this method in your test class AutoCreateEvent_Test
Right now, that is missing from your test class.
 
subodh chaturvedi 17subodh chaturvedi 17
 I did the same thing as you mentioned but is giving me an error :Method does not exist or incorrect signature: void createNewEvent() from the type AutoCreateEvent
Steven NsubugaSteven Nsubuga
@IsTest
public class AutoCreateEvent_Test {

    static testmethod void  CreateEvent(){
      Test.startTest();
        
            User u = new User(
            ProfileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id,
            LastName = 'last',
            Email = 'puser000@amamama.com',
            Username = 'puser000@amamama.com' + System.currentTimeMillis(),
            CompanyName = 'TEST',
            Title = 'title',
            Alias = 'alias',
            TimeZoneSidKey = 'America/Los_Angeles',
            EmailEncodingKey = 'UTF-8',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_US',
            EmployeeNumber='1'
        );
        insert u;
        List<Event> EventRec = new List<Event>();
    
        Account a = new Account(Name='1st Community CU',Membership_Defined__c='Member',Type='Client', ownerid=u.id );
        Insert a;
    
        DealSupportRequest__c dsr = new      DealSupportRequest__c(Account__c=a.Id,StartDateTime__c=system.now(),EndDateTime__c=system.now().addDays(1),ownerid=a.Ownerid);
        Insert dsr;

        List<DealSupportRequest__c> dsreq = new List<DealSupportRequest__c>();
        dsreq.add(dsr);

        Map<Id, DealSupportRequest__c> oldDsreqMap = Map<Id, DealSupportRequest__c>();
        oldDsreqMap.put(dsr.Id, dsr);

        AutoCreateEvent.createNewEvent(dsreq, oldDsreqMap);
         
    Test.stopTest() ;   
    }
}

 
subodh chaturvedi 17subodh chaturvedi 17
i got the understanding that how the reference of the class is called but there are still some error persist at  line no 33-  that not make any sense 
1. Unexpected token 'Map'.
2.Unexpected token 'Map'.
3. Expecting ';' but was: ','
4. Unexpected token ')'.
5.Variable does not exist: DealSupportRequest__c 
 
Steven NsubugaSteven Nsubuga
@IsTest
public class AutoCreateEvent_Test {

    static testmethod void  CreateEvent(){
      Test.startTest();
        
            User u = new User(
            ProfileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id,
            LastName = 'last',
            Email = 'puser000@amamama.com',
            Username = 'puser000@amamama.com' + System.currentTimeMillis(),
            CompanyName = 'TEST',
            Title = 'title',
            Alias = 'alias',
            TimeZoneSidKey = 'America/Los_Angeles',
            EmailEncodingKey = 'UTF-8',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_US',
            EmployeeNumber='1'
        );
        insert u;
        List<Event> EventRec = new List<Event>();
    
        Account a = new Account(Name='1st Community CU',Membership_Defined__c='Member',Type='Client', ownerid=u.id );
        Insert a;
    
        DealSupportRequest__c dsr = new      DealSupportRequest__c(Account__c=a.Id,StartDateTime__c=system.now(),EndDateTime__c=system.now().addDays(1),ownerid=a.Ownerid);
        Insert dsr;

        List<DealSupportRequest__c> dsreq = new List<DealSupportRequest__c>();
        dsreq.add(dsr);

        Map<Id, DealSupportRequest__c> oldDsreqMap = new Map<Id, DealSupportRequest__c>();
        oldDsreqMap.put(dsr.Id, dsr);

        AutoCreateEvent.createNewEvent(dsreq, oldDsreqMap);
         
    Test.stopTest() ;   
    }
}

 
This was selected as the best answer
subodh chaturvedi 17subodh chaturvedi 17
 It works :) Thank You For your help . can you please let me know why these above error are coming  
Steven NsubugaSteven Nsubuga
mark the response as a best answer since it works :)