+ Start a Discussion
sfdc dev 2264sfdc dev 2264 

Dates if condition coverage in test class help needed

Hi,

I have a controller for which i have written a test class which covers only 50%, I am not able to cover the following lines for which i need help on it

//
   if(firstcontract.Contract_Start_Date__c<contractObj.Contract_Start_Date__c && firstcontract.Contract_End_Date__c<contractObj.Contract_End_Date__c ){
                          if(firstcontract.Contract_End_Date__c>=contractObj.Contract_Start_Date__c){
                          firstcontract.Contract_End_Date__c=contractObj.Contract_Start_Date__c.addDays(-1);
                          update firstcontract;//put the list and update
                          }                       
                        }  
                        else{
                        system.debug('elsesection');
                        if(contractObj.Contract_End_Date__c>=firstcontract.Contract_Start_Date__c){
                          contractObj.Contract_End_Date__c=firstcontract.Contract_Start_Date__c.addDays(-1);
                          update contractObj;// put the list and update
                          }    
                          
                          
                          //
                          
                            if(idVsContractmap.containsKey(controbj.Account__r.Id)) {
                List<Contract__c> contrAccList = idVsContractmap.get(controbj.Account__r.Id);
                contrAccList.add(controbj);
                idVsContractmap.put(controbj.Account__c, contrAccList);
            } else {
                idVsContractmap.put(controbj.Account__c, new List<Contract__c> { controbj });
            }   
            //

Kindly help me pls

Thanks in advance
KtHasNoLimitKtHasNoLimit
Hi sfdc dev 2264,

Could you please post your test class? You have to create a record of contarctObjs where start date should in between firstContacts start date and end date then you can cover 

Thanks,
Kirti Rathod
sfdc dev 2264sfdc dev 2264
Hi kirti,

Please find  my test class and apex class for your kind reference
MY TEST CLASS :

@isTest
public class Test_ContractoverlapDateHandler{

    public static testMethod void TestContractdate(){
      Profile pf = [Select Id from Profile where Name = 'System Administrator'];
        User u = new User();
        u.FirstName = 'Test';
        u.LastName = 'User';
        u.Email = 'testuser@test123456789.com';
        u.CompanyName = 'test.com';
        u.Title = 'Test User';
        u.Username = 'testuser@test123456789.com';
        u.Alias = 'testuser';
        u.CommunityNickname = 'Test User';
        u.TimeZoneSidKey = 'Australia/Sydney';
        u.LocaleSidKey = 'en_AU';
        u.EmailEncodingKey = 'ISO-8859-1';
        u.ProfileId = pf.Id;
        u.LanguageLocaleKey = 'en_US';
        insert u;
        
          system.runAs(u){ 
          
          Trigger_Status__c ts=new  Trigger_Status__c();
          ts.Name='ContractTrigger';
          ts.Active__c=true;
          insert ts;
           String prospectRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Prospect Account').getRecordTypeId();

          
                     Account acc = new Account(Name = 'Sample', Active__c = true, Aquire__c = true, Type = 'Prospect Account', 
                                      RecordTypeId = prospectRecordTypeId, Migrated__c = false, 
                                      Estimated_Total_Air_Travel_Spend__c = 0, Manual_Revenue_Update__c = false,
                                      Agency__c = 'N', Dealing_Flag__c = 'N', Aquire_Override__c = 'N' ,Contract_End_Date__c = Date.Today()
                                      );
                                    
                                    
                    insert acc;
                
                    Contact con = new Contact(FirstName = 'Sample', LastName = acc.Name, AccountId = acc.Id);
                
                    insert con;
                
                
                    Opportunity opp = new Opportunity(  Name = 'Opp'+acc.Name , AccountId = acc.Id,
                                              Amount = 500000, Category__c = 'Corporate Airfares', CloseDate = Date.Today(),
                                              StageName = 'Qualify');
                                              
                    insert opp;
                
                    Opportunity opp1 = new Opportunity(  Name = 'Opp1'+acc.Name , AccountId = acc.Id,
                                              Amount = 600000, Category__c = 'Corporate Airfares', CloseDate = Date.Today(),
                                              StageName = 'Qualify');
                                              
                    insert opp1;
                
               
                    Boolean b=true;
                    Proposal__c prop = new Proposal__c(Name = 'Proposal '+opp.Name , Account__c = opp.AccountId, Opportunity__c = opp.Id,
                                              Active__c = true, Type__c = 'Corporate Airfares', International_Annual_Share__c = 70,
                                              Domestic_Annual_Share__c = 90, Frequent_Flyer_Status_Upgrade__c = 'No',
                                              MCA_Routes_Annual_Share__c = 70, Valid_From_Date__c = Date.Today(),
                                              Qantas_Annual_Expenditure__c = 500000, Qantas_Club_Discount__c = 'No',
                                              NAM_Approved__c = false, NAM_Rejected__c = false, Pricing_Approved__c = false, Pricing_Rejected__c = false,
                                              DO_Approved__c = false, DO_Rejected__c = false, TeamLead_Approved__c = false, TeamLead_Rejected__c = false,
                                              Approval_Required_Others__c = false,Status__c='Accepted by Customer',Proposal_End_Date__c=date.today().addmonths(12),Proposal_Start_Date__c=date.today()
                                              );
                    insert prop;
                    
                    Proposal__c prop1 = new Proposal__c(Name = 'Proposal '+opp1.Name , Account__c = opp1.AccountId, Opportunity__c = opp1.Id,
                                              Active__c = true, Type__c = 'Corporate Airfares', International_Annual_Share__c = 70,
                                              Domestic_Annual_Share__c = 90, Frequent_Flyer_Status_Upgrade__c = 'No',
                                              MCA_Routes_Annual_Share__c = 70, Valid_From_Date__c = Date.Today(),
                                              Qantas_Annual_Expenditure__c = 500000, Qantas_Club_Discount__c = 'No',
                                              NAM_Approved__c = false, NAM_Rejected__c = false, Pricing_Approved__c = false, Pricing_Rejected__c = false,
                                              DO_Approved__c = false, DO_Rejected__c = false, TeamLead_Approved__c = false, TeamLead_Rejected__c = false,
                                              Approval_Required_Others__c = false,Status__c='Accepted by Customer',Proposal_End_Date__c=date.today().addmonths(24),Proposal_Start_Date__c=date.today());                
            
                    insert prop1;   
                 
                            
                    Contract__c contr = new Contract__c(Name = 'Contract '+opp.Name , Account__c = prop.Account__c, Opportunity__c = prop.Opportunity__c, Proposal__c = prop.Id,
                                              Active__c = true, Type__c = 'Corporate Airfares', International_Annual_Share__c = 70,
                                              Domestic_Annual_Share__c = 90, Frequent_Flyer_Status_Upgrade__c = 'No', Contracted__c = true,
                                              MCA_Routes_Annual_Share__c = 70, Contract_Start_Date__c = Date.Today(), Contract_End_Date__c = Date.Today().addmonths(12),
                                              Qantas_Annual_Expenditure__c = 500000, Qantas_Club_Discount__c = 'No', Status__c = 'Signature Required by Customer'                                           
                                              );
                                              
                                              
                    insert contr;
                            
                           
               
                    Contract__c contr1 = new Contract__c(Name = 'Contract '+opp1.Name , Account__c = prop1.Account__c, Opportunity__c = prop1.Opportunity__c, Proposal__c = prop1.Id,
                                              Active__c = true, Type__c = 'Corporate Airfares', International_Annual_Share__c = 70,
                                              Domestic_Annual_Share__c = 90, Frequent_Flyer_Status_Upgrade__c = 'No', Contracted__c = true,
                                              MCA_Routes_Annual_Share__c = 70, Contract_Start_Date__c = Date.Today(), Contract_End_Date__c = Date.Today().addmonths(24),
                                              Qantas_Annual_Expenditure__c = 500000, Qantas_Club_Discount__c = 'No', Status__c = 'Signature Required by Customer'                                         
                                              );
                                            
                                              
                    insert contr1;
                    
                    Contract__c contr2 = new Contract__c(Name = 'Contract '+opp1.Name , Account__c = prop1.Account__c, Opportunity__c = prop1.Opportunity__c, Proposal__c = prop1.Id,
                                              Active__c = true, Type__c = 'Corporate Airfares', International_Annual_Share__c = 70,
                                              Domestic_Annual_Share__c = 90, Frequent_Flyer_Status_Upgrade__c = 'No', Contracted__c = true,
                                              MCA_Routes_Annual_Share__c = 70, Contract_Start_Date__c = Date.Today(), Contract_End_Date__c = Date.Today().addmonths(24),
                                              Qantas_Annual_Expenditure__c = 500000, Qantas_Club_Discount__c = 'No', Status__c = 'Signature Required by Customer'                                       
                                              );
                                            
                                              
                    insert contr2;          
     
                }
              
        }     
             
 }


 
MY CONTROLLER:

public class overlapDateHandler{

    public void overlapDateMethod(List<Contract__c> contractList){
    if(checkRecursive.runOnce())
    {

//list<Contract__c> contractList=new list<Contract__c>();
List<Id> conId = new List<Id>();
List<Id> conAccId = new List<Id>();
contractList=Trigger.new;

     for(Contract__c conObj : contractList){
            
            if(conObj.Account__c  != null){
                
                conId.add(conObj.Id);
                conAccId.add(conObj.Account__c);
            }
        }
      list<Contract__c> contrList = new List<Contract__c>(); 
      map<id, list<Contract__c>> idVsContractmap = new map<id, list<Contract__c>>();  
      contrList = [Select name,Contract_Start_Date__c, Contract_End_Date__c, Account__c,Account__r.Id,Status__c From Contract__c where Status__c='Signed by Customer' and Active__c=true and Account__c=: conAccId];
        for(Contract__c controbj : contrList){
            
            if(idVsContractmap.containsKey(controbj.Account__r.Id)) {
                List<Contract__c> contrAccList = idVsContractmap.get(controbj.Account__r.Id);
                contrAccList.add(controbj);
                idVsContractmap.put(controbj.Account__c, contrAccList);
            } else {
                idVsContractmap.put(controbj.Account__c, new List<Contract__c> { controbj });
            }   
            
          }      
        system.debug('contractmap'+idVsContractmap);
       for(Contract__c conObj : contractList){
             
            if(conObj.Account__c != null){   
            system.debug('account'+idVsContractmap.containsKey(conObj.Account__c));
                if(idVsContractmap.containsKey(conObj.Account__c)){
                    Contract__c firstcontract=new Contract__c();
                    for(Contract__c contractObj : idVsContractmap.get(conObj.Account__c)){
                     //Logic Begins for manage start date and end date
                     
                     system.debug('firstcontract'+firstcontract);
                     if(firstcontract.id==null) {
                     firstcontract=contractObj;
                     system.debug('setsfirstcontract');
                     }
                     else{
                      system.debug('secondsection'+'secondsection');
                      system.debug('first contract '+firstcontract.name);
                      system.debug('second contract '+contractObj.name);
                      if(firstcontract.Contract_Start_Date__c<contractObj.Contract_Start_Date__c && firstcontract.Contract_End_Date__c<contractObj.Contract_End_Date__c ){
                          if(firstcontract.Contract_End_Date__c>=contractObj.Contract_Start_Date__c){
                          firstcontract.Contract_End_Date__c=contractObj.Contract_Start_Date__c.addDays(-1);
                          update firstcontract;//put the list and update
                          }                       
                        }  
                        else{
                        system.debug('elsesection');
                        if(contractObj.Contract_End_Date__c>=firstcontract.Contract_Start_Date__c){
                          contractObj.Contract_End_Date__c=firstcontract.Contract_Start_Date__c.addDays(-1);
                          update contractObj;// put the list and update
                          }    
                        
                        }
                                               
                    }   
                   }
            }
        } 

}
}
    
    
    }
}

 
KtHasNoLimitKtHasNoLimit
Hi sfdc dev 2264,
 
MY TEST CLASS :

@isTest
public class Test_ContractoverlapDateHandler{

    public static testMethod void TestContractdate(){
      Profile pf = [Select Id from Profile where Name = 'System Administrator'];
        User u = new User();
        u.FirstName = 'Test';
        u.LastName = 'User';
        u.Email = 'testuser@test123456789.com';
        u.CompanyName = 'test.com';
        u.Title = 'Test User';
        u.Username = 'testuser@test123456789.com';
        u.Alias = 'testuser';
        u.CommunityNickname = 'Test User';
        u.TimeZoneSidKey = 'Australia/Sydney';
        u.LocaleSidKey = 'en_AU';
        u.EmailEncodingKey = 'ISO-8859-1';
        u.ProfileId = pf.Id;
        u.LanguageLocaleKey = 'en_US';
        insert u;
        
          system.runAs(u){ 
          
          Trigger_Status__c ts=new  Trigger_Status__c();
          ts.Name='ContractTrigger';
          ts.Active__c=true;
          insert ts;
           String prospectRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Prospect Account').getRecordTypeId();

          
			 Account acc = new Account(Name = 'Sample', Active__c = true, Aquire__c = true, Type = 'Prospect Account', 
							  RecordTypeId = prospectRecordTypeId, Migrated__c = false, 
							  Estimated_Total_Air_Travel_Spend__c = 0, Manual_Revenue_Update__c = false,
							  Agency__c = 'N', Dealing_Flag__c = 'N', Aquire_Override__c = 'N' ,Contract_End_Date__c = Date.Today()
							  );
                                    
                                    
			insert acc;
		
			Contact con = new Contact(FirstName = 'Sample', LastName = acc.Name, AccountId = acc.Id);
		
			insert con;
                
                
			Opportunity opp = new Opportunity(  Name = 'Opp'+acc.Name , AccountId = acc.Id,
									  Amount = 500000, Category__c = 'Corporate Airfares', CloseDate = Date.Today(),
									  StageName = 'Qualify');
									  
			insert opp;
		
			Opportunity opp1 = new Opportunity(  Name = 'Opp1'+acc.Name , AccountId = acc.Id,
									  Amount = 600000, Category__c = 'Corporate Airfares', CloseDate = Date.Today(),
									  StageName = 'Qualify');
									  
			insert opp1;
		
	   
			Boolean b=true;
			Proposal__c prop = new Proposal__c(Name = 'Proposal '+opp.Name , Account__c = opp.AccountId, Opportunity__c = opp.Id,
									  Active__c = true, Type__c = 'Corporate Airfares', International_Annual_Share__c = 70,
									  Domestic_Annual_Share__c = 90, Frequent_Flyer_Status_Upgrade__c = 'No',
									  MCA_Routes_Annual_Share__c = 70, Valid_From_Date__c = Date.Today(),
									  Qantas_Annual_Expenditure__c = 500000, Qantas_Club_Discount__c = 'No',
									  NAM_Approved__c = false, NAM_Rejected__c = false, Pricing_Approved__c = false, Pricing_Rejected__c = false,
									  DO_Approved__c = false, DO_Rejected__c = false, TeamLead_Approved__c = false, TeamLead_Rejected__c = false,
									  Approval_Required_Others__c = false,Status__c='Accepted by Customer',Proposal_End_Date__c=date.today().addmonths(12),Proposal_Start_Date__c=date.today()
									  );
			insert prop;
			
			Proposal__c prop1 = new Proposal__c(Name = 'Proposal '+opp1.Name , Account__c = opp1.AccountId, Opportunity__c = opp1.Id,
									  Active__c = true, Type__c = 'Corporate Airfares', International_Annual_Share__c = 70,
									  Domestic_Annual_Share__c = 90, Frequent_Flyer_Status_Upgrade__c = 'No',
									  MCA_Routes_Annual_Share__c = 70, Valid_From_Date__c = Date.Today(),
									  Qantas_Annual_Expenditure__c = 500000, Qantas_Club_Discount__c = 'No',
									  NAM_Approved__c = false, NAM_Rejected__c = false, Pricing_Approved__c = false, Pricing_Rejected__c = false,
									  DO_Approved__c = false, DO_Rejected__c = false, TeamLead_Approved__c = false, TeamLead_Rejected__c = false,
									  Approval_Required_Others__c = false,Status__c='Accepted by Customer',Proposal_End_Date__c=date.today().addmonths(24),Proposal_Start_Date__c=date.today());                
	
			insert prop1;   
		 
			List<Contract__c> contractList = new List<Contract__c>();
			Contract__c contr = new Contract__c(Name = 'Contract '+opp.Name , Account__c = prop.Account__c, Opportunity__c = prop.Opportunity__c, Proposal__c = prop.Id,
									  Active__c = true, Type__c = 'Corporate Airfares', International_Annual_Share__c = 70,
									  Domestic_Annual_Share__c = 90, Frequent_Flyer_Status_Upgrade__c = 'No', Contracted__c = true,
									  MCA_Routes_Annual_Share__c = 70, Contract_Start_Date__c = Date.Today(), Contract_End_Date__c = Date.Today().addmonths(12),
									  Qantas_Annual_Expenditure__c = 500000, Qantas_Club_Discount__c = 'No', Status__c = 'Signature Required by Customer'                                           
									  );
									  
									  
			insert contr;
					
                           
               
			Contract__c contr1 = new Contract__c(Name = 'Contract '+opp1.Name , Account__c = prop1.Account__c, Opportunity__c = prop1.Opportunity__c, Proposal__c = prop1.Id,
									  Active__c = true, Type__c = 'Corporate Airfares', International_Annual_Share__c = 70,
									  Domestic_Annual_Share__c = 90, Frequent_Flyer_Status_Upgrade__c = 'No', Contracted__c = true,
									  MCA_Routes_Annual_Share__c = 70, Contract_Start_Date__c = Date.Today(), Contract_End_Date__c = Date.Today().addmonths(7),
									  Qantas_Annual_Expenditure__c = 500000, Qantas_Club_Discount__c = 'No', Status__c = 'Signature Required by Customer'                                         
									  );
									
									  
			insert contr1;
			
			Contract__c contr2 = new Contract__c(Name = 'Contract '+opp1.Name , Account__c = prop1.Account__c, Opportunity__c = prop1.Opportunity__c, Proposal__c = prop1.Id,
									  Active__c = true, Type__c = 'Corporate Airfares', International_Annual_Share__c = 70,
									  Domestic_Annual_Share__c = 90, Frequent_Flyer_Status_Upgrade__c = 'No', Contracted__c = true,
									  MCA_Routes_Annual_Share__c = 70, Contract_Start_Date__c = Date.Today()+addmonths(2), Contract_End_Date__c = Date.Today().addmonths(8),
									  Qantas_Annual_Expenditure__c = 500000, Qantas_Club_Discount__c = 'No', Status__c = 'Signature Required by Customer'                                       
									  );
									
									  
			
			insert contr2;

		}
              
        }     
             
 }

Please check and let me know .
Thanks,
Kirti Rathod
sfdc dev 2264sfdc dev 2264
no its still the same , with 50 % only
sfdc dev 2264sfdc dev 2264
Hi kirti, No the coverage has not increased still the same 50% only
KtHasNoLimitKtHasNoLimit
Hi sfdc dev 2264,

Try to use System.debugs in controller and find where the condition is not matched.