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
sfdc_beginner7sfdc_beginner7 

Test class is covering only 36% of code?

I am new to test classes. My Test class is giving only 36% coverage.What else i  can check in my test class

Test class:

@isTest
public class ITOVC_TargetMasterGovernanceClassTest {
    @TestSetup
    static void setup(){
        ITOVC_Target__c target1 = new ITOVC_Target__c(Name='TG01',Start_Date__c=Datetime.newInstance(2018, 01, 01), End_Date__c=Datetime.newInstance(2018, 01, 31),Target_Value__c=1);
        insert target1;
    }
  @isTest  static void testDuplicateRecordsForSameStartEndDate(){
        ITOVC_Target__c target2 = new ITOVC_Target__c(Name='TG01',Start_Date__c=Datetime.newInstance(2018, 01, 01), End_Date__c=Datetime.newInstance(2018, 01, 31),Target_Value__c=1);
        try {
           insert target2;
       } catch (Exception e) { 
           System.debug('An error happened, as predicted!');
       }
    }
}
apex class:

public class ITOVC_TargetMasterGovernanceClass {
    public void DuplicateRecordsForSameStartEndDate(List<ITOVC_Target__c> newTgId){
        List<ITOVC_Target__c> TgList = ([Select Name,Target_Value__c,Start_date__c,End_date__c from ITOVC_Target__c limit 10000]);
        for(ITOVC_Target__c tg : TgList){
            system.debug('ITOVC_Target__c tg :  ' + tg);
            for(ITOVC_Target__c newTg : newTgId ){
                system.debug('ITOVC_Target__c newtg :  ' + newtg);
                if(tg.Start_Date__c !=Null || tg.End_Date__c !=Null){
                    if( 
                        ((newTg.End_Date__c >= tg.Start_Date__c && newTg.End_Date__c <= tg.End_Date__c) ||
                        (newTg.Start_Date__c >= tg.Start_Date__c && newTg.End_Date__c <= tg.End_Date__c)||
                        (newTg.Start_Date__c >= tg.Start_Date__c && newTg.Start_Date__c <= tg.End_Date__c)||
                        (newTg.Start_Date__c < tg.Start_Date__c && newTg.End_Date__c > tg.End_Date__c))&&
                        (newTg.Name >= tg.Name)&&
                              (newTg.Target_Value__c >= tg.Target_Value__c)
                      ){

                              newTg.addError('Start date and End date are getting overlapped with existing records.');

                       }
                 }
            }
     }
    }
    public void recordsCantBeDeleted(List<ITOVC_Target__c> oldTgId){

        for(ITOVC_Target__c TgDel : oldTgId){

            TgDel.addError('You can\'t delete master target record');
        }
    }
}
apex trigger:

trigger ITOVC_TargetMasterGovernanceTrigger on ITOVC_Target__c (before insert,before update, before delete) {
     ITOVC_TargetMasterGovernanceClass thClass = new ITOVC_TargetMasterGovernanceClass();
    if(trigger.isInsert || Trigger.isUpdate){
        system.debug('Trigger.new contains :   '+Trigger.new);
        thClass.DuplicateRecordsForSameStartEndDate(Trigger.new);
    }
   if(Trigger.isDelete)
    {
        thClass.recordsCantBeDeleted(Trigger.old);
        system.debug('Trigger.old contains :   '+Trigger.old);
    }

}
 
Colin KenworthyColin Kenworthy
Launch the Developer Console from the links in the top right corner.  In Developer Console look at the "Tests" tab in the bottom row of tabs.  Locate your class showing 36% coverage and double-click it.  It will display the class with lines colored red if they did not run in your test methods.

I would start by making sure all the conditions in your 7-line IF statement are tested (or put them all onto one long line).
You should also test deleting a record.