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
adroitusadroitus 

TestMethod for Trigger

 

Hello Developer!

 

 

 I am littlebit confused how to write testmethod  for particular trigger.

And, Done some part of coding in testmethod but it gives only 36% code coverage but i need atleast 75%

 

what to write on code in testmethod to make atleast 75%.

 

Test method:

 

@istest
private class leave_testmethod
{
static testmethod void leaveprocess_testmethod()
{
   integer totalDaysFinal;
   Leave_Process__c leavetest= new Leave_Process__c();
   
test.startTest();

insert leavetest;


test.stopTest();


}
}

 

Trigger:

 

trigger trgLeaveAppBeforeInsert on Leave_Process__c(before insert) {
      
    Date startDate, endDate;
    Integer startDayofYear, endDayofYear, totalDaysFinal,totalDaysInitial;
    if(trigger.isInsert){
        for(Leave_Process__c obj : Trigger.new){
          
          
            startDate = obj.manage_packapp__Start_Date__c;
            endDate = obj.manage_packapp__End_date__c;
            // remainingLeave = obj.managed_Appsamp_Remaining_Leave_c;
          
            if(startDate.year() == endDate.year()){
              
                totalDaysInitial = endDate.dayOfYear() - startDate.dayOfYear()+1;
                totalDaysFinal = totalDaysInitial;
               
               DateTime startEndDate;
                for(integer i = 0; i< totalDaysFinal; i++){
                  
                    for (Holiday hObj : [Select activityDate from Holiday]){
                      
                       if(startDate.addDays(i) == hObj.activityDate){
                          
                            totalDaysFinal--;
                        }                      
                    }
                  
                    
                   
                }
                 // Code for calculating weekend days
                for(integer j = 0; j< totalDaysFinal; j++)
                {
                    startEndDate = startDate.addDays(j);
                    if(startEndDate.format('E')=='Sat'|startEndDate.format('E')=='Sun')
                    {
                        totalDaysFinal--;
                    }
                                    
                }
            }
            obj.manage_packapp__leaveprocess__c = totalDaysFinal;
           // obj.managed_Appsamp_Remaining_Leave_c=obj.managed_Appsamp_Remaining_Leave_c - totalDaysFinal;
          
        }

  }
}

 

 

plz help us out.

 

Thanks & Regards

Ashish Agrawal

Best Answer chosen by Admin (Salesforce Developers) 
ashish raiashish rai

Hello,

   Use this but dont forget to mention your mendotry filed before inserting Leave_Process__c record.

@istest
private class leave_testmethod
{
    static testmethod void leaveprocess_testmethod()
    {         
       Date startDate, endDate;
       Integer startDayofYear, endDayofYear, totalDaysFinal,totalDaysInitial;
       integer totalDaysFinal;
       Leave_Process__c leavetest= new Leave_Process__c(manage_packapp__Start_Date__c=system.today(),manage_packapp__End_date__c=system.today(),other mendetory fields);
       insert leavetest;

    }
}

 

Think this will telp you.

All Answers

ashish raiashish rai

Hello,

   Use this but dont forget to mention your mendotry filed before inserting Leave_Process__c record.

@istest
private class leave_testmethod
{
    static testmethod void leaveprocess_testmethod()
    {         
       Date startDate, endDate;
       Integer startDayofYear, endDayofYear, totalDaysFinal,totalDaysInitial;
       integer totalDaysFinal;
       Leave_Process__c leavetest= new Leave_Process__c(manage_packapp__Start_Date__c=system.today(),manage_packapp__End_date__c=system.today(),other mendetory fields);
       insert leavetest;

    }
}

 

Think this will telp you.

This was selected as the best answer
vishal@forcevishal@force

Ashish is right!

 

you haven't assigned any field values on your insert, hence your statements are not getting covered.

Do as he said, give a value to start and end date and then check, it should ideally cover 100%

 

 

Leave_Process__c leavetest= new Leave_Process__c(Name = 'test',manage_packapp__Start_Date__c= date.today(),   manage_packapp__Start_Date__c= date.today().addMonths(1));   

 

only then you should be inserting leavetest.

adroitusadroitus

Hello,

 

 

Thanks its working now!!!

 

But there is confusion it shows your trigger coverge is 100% but it also shows that all your average apex class and trigger is 17%(total code coverage)

 

i want 2 know that testmethod is in 100% or 17%.

ashish raiashish rai

Hello,

    Well your trigger is 100% cover. The message show the average of the all calss used in your Org as well as Trigger.But when you will deploy this inside a production Org you need 75% coverage of your Class and Trigger. So now its upto that what you want to deploy inside prodution Org. If  only Trigger then you dont have to write the test method for class but if you want to deploy some classes also then your average of all class + Trigger should be 75%. 

Please go through the Test methods Reqirement and why we use this so that things become clear to you.

 

adroitusadroitus

Thanks ashish!!!

 

 

I understand well .... and trigger is working fine.

 

 

Thanks & Regards

Ashish Agrawal