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
miiWorksmiiWorks 

Apex Test Class Only 35%

Hello

 

I am very new to writing test classes, could someone please give me some direction on why this test clasee is only achieving 35% code coverage?

 

Thank you!

 

public class PresentationWithTicketTypeController {
 
    //added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
     // add the instance for the variables being passed by id on the url
    private miiEventV1__Presentation__c pres {get;set;}
    // set the id of the record that is created -- ONLY USED BY THE TEST CLASS
    public ID newRecordId {get;set;}
 
    // initialize the controller
    public PresentationWithTicketTypeController(ApexPages.StandardController controller) {
 
        //initialize the stanrdard controller
        this.controller = controller;
        // load the current record
        pres = (miiEventV1__Presentation__c)controller.getRecord();
 
    }
 
    // method called from the VF's action attribute to clone the pres
    public PageReference cloneWithItems() {
 
         // setup the save point for rollback
         Savepoint sp = Database.setSavepoint();
         miiEventV1__Presentation__c newPRES;
 
         try {
 
              //copy the project plan - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             pres = [Select Id, RecordTypeId, miiEventV1__Status__c, miiEventV1__Presentation_Type__c, miiEventV1__Active__c, miiEventV1__Actual_Duraction_Mins__c, miiEventV1__Booking_Link__c, miiEventV1__Breaks_and_Refreshments__c, miiEventV1__Date_Publish_Media__c, miiEventV1__Date_of_Presentation__c, miiEventV1__Display_Name__c, miiEventV1__GoToWebinar_Prefix__c, miiEventV1__Long_Description__c, miiEventV1__Media_Description__c, miiEventV1__Media_Duration_MM_SS__c, miiEventV1__Media_URL__c, miiEventV1__Presentation_Times__c, miiEventV1__Presentation_Type_Display_Name__c, miiEventV1__Short_Description__c, miiEventV1__Tags__c, miiEventV1__Time_Zone__c, miiEventV1__Venue_Room__c, miiEventV1__Venue__c, miiEventV1__Video_Domain__c, miiEventV1__Video_Height__c, miiEventV1__Video_Type__c, miiEventV1__Video_Width__c, miiEventV1__Webinar_ID__c, miiEventV1__Webinar_Maximum_Registrations__c, miiEventV1__What_You_Will_Learn__c, Media_Channel_ID__c, Amazon_Bucket__c, Amazon_File_Name__c, Media_Centre_Program_Name__c, YouTube_Video_ID__c, YouTube_Display_in_HD__c, Start_Video_at__c, Record_Type_Custom__c, Viewing_Security__c FROM miiEventV1__Presentation__c where id = :pres.id];
             newPRES = pres.clone(false);
             newPRES.miiEventV1__Status__c = 'Planned';
             newPRES.miiEventV1__Active__c = true;             
             newPRES.miiEventV1__Video_Type__c = '';     
             newPRES.miiEventV1__Media_Description__c = 'TBA'; 
             newPRES.miiEventV1__Media_Duration_MM_SS__c = 00.00; 
             newPRES.miiEventV1__Media_URL__c = ''; 
             newPRES.miiEventV1__Tags__c = ''; 
             newPRES.Start_Video_at__c = NULL;
             newPRES.miiEventV1__Date_Publish_Media__c = NULL;
             newPRES.Amazon_File_Name__c = ''; 
             newPRES.YouTube_Video_ID__c = '';         
           
             insert newPRES;
 
             // set the id of the new pres created for testing
             newRecordId = newPRES.id;
 
             // copy over the child record - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             List<miiEventV1__Presentation_Ticket_Type__c> tickettypes = new List<miiEventV1__Presentation_Ticket_Type__c>();
             for (miiEventV1__Presentation_Ticket_Type__c ptt : [Select Id, Name, miiEventV1__Presentation__c, miiEventV1__Active__c, miiEventV1__Amount_Per_Ticket__c, miiEventV1__Maximum_Tickets_Available__c, miiEventV1__Ticket_Type_Internal_Name__c, miiEventV1__Ticket_Type_Website_Name__c, miiEventV1__Ticket_Type__c, miiEventV1__Total_Expected_Revenue__c, Product_For_Sale__c, Ticket_Image_URL__c, Display_Groups__c FROM miiEventV1__Presentation_Ticket_Type__c where miiEventV1__Presentation__c = :pres.id]) {
                  miiEventV1__Presentation_Ticket_Type__c newPTT = ptt.clone(false);
                  newPTT.miiEventV1__Presentation__c = newPRES.id;
                  tickettypes.add(newPTT);
             }
             
             // copy over the child record - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
              List<miiEventV1__Speaker2Presentation__c> speakers = new List<miiEventV1__Speaker2Presentation__c>();
             for (miiEventV1__Speaker2Presentation__c psp : [Select miiEventV1__Presentation__c, miiEventV1__Speaker__c, miiEventV1__Active__c FROM miiEventV1__Speaker2Presentation__c where miiEventV1__Presentation__c = :pres.id]) {
                  miiEventV1__Speaker2Presentation__c newPSP = psp.clone(false);
                  newPSP.miiEventV1__Presentation__c = newPRES.id;
                  speakers.add(newPSP);
             }                                        
             
             insert tickettypes;
             insert speakers;
 
         } catch (Exception e){
             // roll everything back in case of error
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
         }
 
        //return new PageReference('/apex/PresentationView?id='+newPRES.id);
          return new PageReference('/'+newPRES.id);
    }

 public static testmethod void Test1()

    {

    miiEventV1__Presentation__c p=new miiEventV1__Presentation__c();

    ApexPages.StandardController sc = new ApexPages.standardController(p);     

    PresentationWithTicketTypeController pres=new PresentationWithTicketTypeController (sc);

    pres.cloneWithItems();

    }    
         
 
}

 

Suresh RaghuramSuresh Raghuram

you are not covering the try block and for loop

 

assume you are passing values and  doing insert for the following fields. give some input relevant to the datatype and field type.

work on for loop too.

 

newPRES = pres.clone(false);
             newPRES.miiEventV1__Status__c = 'Planned';
             newPRES.miiEventV1__Active__c = true;             
             newPRES.miiEventV1__Video_Type__c = '';     
             newPRES.miiEventV1__Media_Description__c = 'TBA'; 
             newPRES.miiEventV1__Media_Duration_MM_SS__c = 00.00; 
             newPRES.miiEventV1__Media_URL__c = ''; 
             newPRES.miiEventV1__Tags__c = ''; 
             newPRES.Start_Video_at__c = NULL;
             newPRES.miiEventV1__Date_Publish_Media__c = NULL;
             newPRES.Amazon_File_Name__c = ''; 
             newPRES.YouTube_Video_ID__c = '';         
           
             insert newPRES;

 

miiWorksmiiWorks

Hi Suree


Thanks for that

 

Im not really sure what you mean sorry. I put this code into 1 line now and its up to 48% coverage


Would you mind giving me an example of how to fix this?

 

I did the following change

 

public class PresentationWithTicketTypeController {
 
    //added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
     // add the instance for the variables being passed by id on the url
    private miiEventV1__Presentation__c pres {get;set;}
    // set the id of the record that is created -- ONLY USED BY THE TEST CLASS
    public ID newRecordId {get;set;}
 
    // initialize the controller
    public PresentationWithTicketTypeController(ApexPages.StandardController controller) {
 
        //initialize the stanrdard controller
        this.controller = controller;
        // load the current record
        pres = (miiEventV1__Presentation__c)controller.getRecord();
 
    }
 
    // method called from the VF's action attribute to clone the pres
    public PageReference cloneWithItems() {
 
         // setup the save point for rollback
         Savepoint sp = Database.setSavepoint();
         miiEventV1__Presentation__c newPRES;
 
         try {
 
              //copy the project plan - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             pres = [Select Id, RecordTypeId, miiEventV1__Status__c, miiEventV1__Presentation_Type__c, miiEventV1__Active__c, miiEventV1__Actual_Duraction_Mins__c, miiEventV1__Booking_Link__c, miiEventV1__Breaks_and_Refreshments__c, miiEventV1__Date_Publish_Media__c, miiEventV1__Date_of_Presentation__c, miiEventV1__Display_Name__c, miiEventV1__GoToWebinar_Prefix__c, miiEventV1__Long_Description__c, miiEventV1__Media_Description__c, miiEventV1__Media_Duration_MM_SS__c, miiEventV1__Media_URL__c, miiEventV1__Presentation_Times__c, miiEventV1__Presentation_Type_Display_Name__c, miiEventV1__Short_Description__c, miiEventV1__Tags__c, miiEventV1__Time_Zone__c, miiEventV1__Venue_Room__c, miiEventV1__Venue__c, miiEventV1__Video_Domain__c, miiEventV1__Video_Height__c, miiEventV1__Video_Type__c, miiEventV1__Video_Width__c, miiEventV1__Webinar_ID__c, miiEventV1__Webinar_Maximum_Registrations__c, miiEventV1__What_You_Will_Learn__c, Media_Channel_ID__c, Amazon_Bucket__c, Amazon_File_Name__c, Media_Centre_Program_Name__c, YouTube_Video_ID__c, YouTube_Display_in_HD__c, Start_Video_at__c, Record_Type_Custom__c, Viewing_Security__c FROM miiEventV1__Presentation__c where id = :pres.id];
             newPRES = pres.clone(false); newPRES.miiEventV1__Status__c = 'Planned'; newPRES.miiEventV1__Active__c = true; newPRES.miiEventV1__Video_Type__c = ''; newPRES.miiEventV1__Media_Description__c = 'TBA'; newPRES.miiEventV1__Media_Duration_MM_SS__c = 00.00; newPRES.miiEventV1__Media_URL__c = ''; newPRES.miiEventV1__Tags__c = ''; newPRES.Start_Video_at__c = NULL; newPRES.miiEventV1__Date_Publish_Media__c = NULL; newPRES.Amazon_File_Name__c = ''; newPRES.YouTube_Video_ID__c = '';         
           
             insert newPRES;
 
             // set the id of the new pres created for testing
             newRecordId = newPRES.id;
 
             // copy over the child record - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             List<miiEventV1__Presentation_Ticket_Type__c> tickettypes = new List<miiEventV1__Presentation_Ticket_Type__c>();
             for (miiEventV1__Presentation_Ticket_Type__c ptt : [Select Id, Name, miiEventV1__Presentation__c, miiEventV1__Active__c, miiEventV1__Amount_Per_Ticket__c, miiEventV1__Maximum_Tickets_Available__c, miiEventV1__Ticket_Type_Internal_Name__c, miiEventV1__Ticket_Type_Website_Name__c, miiEventV1__Ticket_Type__c, miiEventV1__Total_Expected_Revenue__c, Product_For_Sale__c, Ticket_Image_URL__c, Display_Groups__c FROM miiEventV1__Presentation_Ticket_Type__c where miiEventV1__Presentation__c = :pres.id]) {
                  miiEventV1__Presentation_Ticket_Type__c newPTT = ptt.clone(false);
                  newPTT.miiEventV1__Presentation__c = newPRES.id;
                  tickettypes.add(newPTT);
             }
             
             // copy over the child record - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
              List<miiEventV1__Speaker2Presentation__c> speakers = new List<miiEventV1__Speaker2Presentation__c>();
             for (miiEventV1__Speaker2Presentation__c psp : [Select miiEventV1__Presentation__c, miiEventV1__Speaker__c, miiEventV1__Active__c FROM miiEventV1__Speaker2Presentation__c where miiEventV1__Presentation__c = :pres.id]) {
                  miiEventV1__Speaker2Presentation__c newPSP = psp.clone(false);
                  newPSP.miiEventV1__Presentation__c = newPRES.id;
                  speakers.add(newPSP);
             }                                        
             
             insert tickettypes;
             insert speakers;
 
         } catch (Exception e){
             // roll everything back in case of error
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
         }
 
        //return new PageReference('/apex/PresentationView?id='+newPRES.id);
          return new PageReference('/'+newPRES.id);
    }

 public static testmethod void Test1()

    {

    miiEventV1__Presentation__c p=new miiEventV1__Presentation__c();

    ApexPages.StandardController sc = new ApexPages.standardController(p);     

    PresentationWithTicketTypeController pres=new PresentationWithTicketTypeController (sc);

    pres.cloneWithItems();

    }    
         
 
}

 

Rajesh SriramuluRajesh Sriramulu

Hi

 

 

Insert the miiEventV1__Presentation__c fields  which are in query.

 

 

Regards,

Rajesh.

miiWorksmiiWorks

Hi Rajesh

 

I am still unable to get this working, ive been on it for hours!

 

Would it be possible for you to post the test class, or PM me for consulting to do some work?

 

Here is what I have

 

public class PresentationWithTicketTypeController {
 
    //added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
     // add the instance for the variables being passed by id on the url
    private miiEventV1__Presentation__c pres {get;set;}
    // set the id of the record that is created -- ONLY USED BY THE TEST CLASS
    public ID newRecordId {get;set;}
 
    // initialize the controller
    public PresentationWithTicketTypeController(ApexPages.StandardController controller) {
 
        //initialize the stanrdard controller
        this.controller = controller;
        // load the current record
        pres = (miiEventV1__Presentation__c)controller.getRecord();
 
    }
 
    // method called from the VF's action attribute to clone the pres
    public PageReference cloneWithItems() {
 
         // setup the save point for rollback
         Savepoint sp = Database.setSavepoint();
         miiEventV1__Presentation__c newPRES;
 
         try {
 
              //copy the project plan - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             pres = [Select Id, RecordTypeId, miiEventV1__Status__c, miiEventV1__Presentation_Type__c, miiEventV1__Active__c, miiEventV1__Actual_Duraction_Mins__c, miiEventV1__Booking_Link__c, miiEventV1__Breaks_and_Refreshments__c, miiEventV1__Date_Publish_Media__c, miiEventV1__Date_of_Presentation__c, miiEventV1__Display_Name__c, miiEventV1__GoToWebinar_Prefix__c, miiEventV1__Long_Description__c, miiEventV1__Media_Description__c, miiEventV1__Media_Duration_MM_SS__c, miiEventV1__Media_URL__c, miiEventV1__Presentation_Times__c, miiEventV1__Presentation_Type_Display_Name__c, miiEventV1__Short_Description__c, miiEventV1__Tags__c, miiEventV1__Time_Zone__c, miiEventV1__Venue_Room__c, miiEventV1__Venue__c, miiEventV1__Video_Domain__c, miiEventV1__Video_Height__c, miiEventV1__Video_Type__c, miiEventV1__Video_Width__c, miiEventV1__Webinar_ID__c, miiEventV1__Webinar_Maximum_Registrations__c, miiEventV1__What_You_Will_Learn__c, Media_Channel_ID__c, Amazon_Bucket__c, Amazon_File_Name__c, Media_Centre_Program_Name__c, YouTube_Video_ID__c, YouTube_Display_in_HD__c, Start_Video_at__c, Record_Type_Custom__c, Viewing_Security__c FROM miiEventV1__Presentation__c where id = :pres.id];
             newPRES = pres.clone(false);
             newPRES.miiEventV1__Status__c = 'Planned';
             newPRES.miiEventV1__Active__c = true; 
             newPRES.miiEventV1__Video_Type__c = ''; 
             newPRES.miiEventV1__Media_Description__c = 'TBA'; 
             newPRES.miiEventV1__Media_Duration_MM_SS__c = 00.00; 
             newPRES.miiEventV1__Media_URL__c = ''; 
             newPRES.miiEventV1__Tags__c = ''; 
             newPRES.Start_Video_at__c = NULL; 
             newPRES.miiEventV1__Date_Publish_Media__c = NULL; 
             newPRES.Amazon_File_Name__c = ''; 
             newPRES.YouTube_Video_ID__c = '';        
           
             insert newPRES;
 
             // set the id of the new pres created for testing
             newRecordId = newPRES.id;
 
             // copy over the child record - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             List<miiEventV1__Presentation_Ticket_Type__c> tickettypes = new List<miiEventV1__Presentation_Ticket_Type__c>();
             for (miiEventV1__Presentation_Ticket_Type__c ptt : [Select Id, Name, miiEventV1__Presentation__c, miiEventV1__Active__c, miiEventV1__Amount_Per_Ticket__c, miiEventV1__Maximum_Tickets_Available__c, miiEventV1__Ticket_Type_Internal_Name__c, miiEventV1__Ticket_Type_Website_Name__c, miiEventV1__Ticket_Type__c, miiEventV1__Total_Expected_Revenue__c, Product_For_Sale__c, Ticket_Image_URL__c, Display_Groups__c FROM miiEventV1__Presentation_Ticket_Type__c where miiEventV1__Presentation__c = :pres.id]) {
                  miiEventV1__Presentation_Ticket_Type__c newPTT = ptt.clone(false);
                  newPTT.miiEventV1__Presentation__c = newPRES.id;
                  tickettypes.add(newPTT);
             }
             
             // copy over the child record - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
              List<miiEventV1__Speaker2Presentation__c> speakers = new List<miiEventV1__Speaker2Presentation__c>();
             for (miiEventV1__Speaker2Presentation__c psp : [Select miiEventV1__Presentation__c, miiEventV1__Speaker__c, miiEventV1__Active__c FROM miiEventV1__Speaker2Presentation__c where miiEventV1__Presentation__c = :pres.id]) {
                  miiEventV1__Speaker2Presentation__c newPSP = psp.clone(false);
                  newPSP.miiEventV1__Presentation__c = newPRES.id;
                  speakers.add(newPSP);
             }                                        
             
             insert tickettypes;
             insert speakers;
 
         } catch (Exception e){
             // roll everything back in case of error
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
         }
 
        //return new PageReference('/apex/PresentationView?id='+newPRES.id);
          return new PageReference('/'+newPRES.id);
    }

 public static testmethod void Test1()

    {
    miiEventV1__Presentation__c p=new miiEventV1__Presentation__c();
    ApexPages.StandardController sc = new ApexPages.standardController(p);     
    PresentationWithTicketTypeController pres=new PresentationWithTicketTypeController (sc);

    pres.cloneWithItems();
  
    
    }    
  
}

 

 

 

Rajesh SriramuluRajesh Sriramulu

Hi

 

see this

 

public static testmethod void Test1()

    {
    miiEventV1__Presentation__c p=new miiEventV1__Presentation__c();

p.miiEventV1__Status__c'Open';
p.miiEventV1__Presentation_Type__c='A';

p.miiEventV1__Active__c=

 

like this insert the fields values here

insert p;

 

miiEventV1__Presentation_Ticket_Type__c  mptt = new miiEventV1__Presentation_Ticket_Type__c();

mptt.miiEventV1__Presentation__c='test';

mptt.

mppt.

like this insert the fields values here

insert mptt;

 

miiEventV1__Speaker2Presentation__c msp = new miiEventV1__Speaker2Presentation__c();

msp.miiEventV1__Active__c='yes';

msp.

msp.like this insert the fields values here

insert msp;

 

   ApexPages.StandardController sc = new ApexPages.standardController(p);     
    PresentationWithTicketTypeController pres=new PresentationWithTicketTypeController (sc);

    pres.cloneWithItems();
 
    
    }    
 
}