+ Start a Discussion
Luke Higgins 22Luke Higgins 22 

Writing a test class for a Trigger (stuck at 56% code coverage)

I have written a subpar test class for a trigger that doesn't cover lines 33-50 on the trigger.

Trigger code:
trigger autoAddNAM_VAM on ts2__Placement__c (after insert) {
    List<jstcl__PlacementTeamMember__c> teamMems = new List<jstcl__PlacementTeamMember__c>();
     Set<Id> tsClientSet = new Set<id>();
      Map<ID, Account> tsClientmap = new Map<ID, Account> (); 
    Map<ID, Account> tsClientmap2 = new Map<ID, Account> (); 
    
    
    
 for(ts2__Placement__c newTeamMem : Trigger.New)
    {
        
        if(newTeamMem.ts2__Client__c != null)
        {
            
            tsClientSet.add(newTeamMem.ts2__Client__c);
        }
    }
    
    if(tsClientSet.size() > 0)
    {
        tsClientmap = new Map<ID, Account>([SELECT Id, National_Account_Manager__c from Account where Id IN: tsClientSet]);
        tsClientmap2 = new Map<ID, Account>([SELECT Id, Vertical_Account_Manager__c	from Account where Id IN: tsClientSet]);
    }
    for(ts2__Placement__c newTeamMem : Trigger.New){

        if(tsClientmap.Containskey(newTeamMem.ts2__Client__C))
        {
             System.debug('tsClientmap = ' + tsClientmap);
            System.debug('tsClientmap2 = ' + tsClientmap2);
                      
  try{ 
            if (tsClientmap.get(newTeamMem.ts2__Client__C).National_Account_Manager__c != null ){
                teamMems.add(new jstcl__PlacementTeamMember__c(
                        jstcl__Placement__c = newTeamMem.Id,
                        jstcl__User__c = tsClientmap.get(newTeamMem.ts2__Client__C).National_Account_Manager__c,
                        jstcl__CommissionPlan__c = [Select jstcl__Commission_Plan__c
                                                    FROM jstcl__TG_Commission_Plan_Assignment__c 
                                                    WHERE jstcl__Commission_Plan__r.Name LIKE '%NVM%' AND jstcl__User__c = :tsClientmap.get(newTeamMem.ts2__Client__C).National_Account_Manager__c LIMIT 1].jstcl__Commission_Plan__c,
                        jstcl__SplitPercent__c = 100));
            }
      else if (tsClientmap2.get(newTeamMem.ts2__Client__C).Vertical_Account_Manager__c != null ){
                teamMems.add(new jstcl__PlacementTeamMember__c(
                        jstcl__Placement__c = newTeamMem.Id,
                        jstcl__User__c = tsClientmap2.get(newTeamMem.ts2__Client__C).Vertical_Account_Manager__c,
                        jstcl__CommissionPlan__c = [Select jstcl__Commission_Plan__c
                                                    FROM jstcl__TG_Commission_Plan_Assignment__c 
                                                    WHERE jstcl__Commission_Plan__r.Name LIKE '%NVM%' AND jstcl__User__c = :tsClientmap2.get(newTeamMem.ts2__Client__C).Vertical_Account_Manager__c LIMIT 1].jstcl__Commission_Plan__c,
                        jstcl__SplitPercent__c = 100));
            }
  }catch(exception e){
       System.debug('The following exception has occurred: ' + e.getMessage());
  }   
        
    }
   
    insert teamMems;
    }
}
Test class so far:
@isTest
private class addNAM_VAMtest{
    private static testMethod void testNAM_VAM(){
        Test.startTest();
        ADP_Department__c adp = new ADP_Department__c();
        adp.Name = 'Georgia';
        insert adp;
         Contact con=new Contact(
            FirstName='fname',
            LastName = 'lname',
            Email = 'email@gmail.com',
            Phone = '9743800309'); 
        insert con; 
       
        ts2__Job__c job = new ts2__Job__c();
        job.Name = 'Software Engineer';
        insert job;
        Account acc = new Account(Name = 'Time Warner Inc.');
        insert acc;
        ts2__HolidayCalendar__c hcal = new ts2__HolidayCalendar__c();
        hcal.Name= 'Holiday Calendar';
        insert hcal;
        Labor_Category__c lcat = new Labor_Category__c(Name='Software Engineer 1');  
        insert lcat;
         ts2__Placement__c plc = new ts2__Placement__c();
        plc.ts2__Job__c = job.Id;
        plc.Contractor_Type__c = 'Technical Youth';
        plc.Onboarding_Method__c= 'Staff Augmentation';
        plc.ts2__Start_Date__c= Date.newInstance(2019, 1, 7);
        plc.ts2__End_Date__c= Date.newInstance(2019, 2, 15);
        plc.jstcl__HireType__c= 'W2';
        plc.ADP_Department__c= adp.Id;
        plc.ts2__Bill_Rate__c= 60.00;
        plc.ts2__Pay_Rate__c= 30.00;
        plc.jstcl__Invoice_Batch__c= 'Placement';
        plc.ts2__Client__c= acc.Id;
        plc.ts2__Hiring_Manager__c= con.Id;
        plc.jstcl__TimecardApprover__c= con.Id;
        plc.jstcl__HolidayCalendar__c= hcal.Id;
		plc.State_Worked_In_Non_TFI_Placements__c= 'GA - Georgia';
        plc.jstcl__Overtime_Bill_Rate__c= 60;
        plc.jstcl__Burden__c= 0.20;
        plc.jstcl__Accounts_Receivable_U__c= '00537000005d7F2AAI';
        plc.jstcl__Terms__c= 'Net 60';
        plc.ts2__Employee__c= con.Id;
        plc.Labor_Category__c= lcat.Id;
        
        insert plc;
        User u = new User(
 	    ProfileId = [SELECT Id FROM Profile WHERE Name = 'National Sales - Brooksource'].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'
		);
         
        jstcl__TG_Commission_Plan__c cp = new jstcl__TG_Commission_Plan__c(name = 'National Vertical Manager (NVM or NAM)');
        insert cp;
        
        
        
        System.debug('>>>>>>>>> Starting Test');
        jstcl__PlacementTeamMember__c ptm = new jstcl__PlacementTeamMember__c(jstcl__Placement__c = plc.Id,
                                                                              jstcl__User__c = u.Id,
                                                                    //          jstcl__CommissionPlan__c = cp.Id,
                                                                              jstcl__SplitPercent__c = 100);
        insert ptm;
        
        System.assertEquals(ptm.jstcl__User__c, acc.National_Account_Manager__c);
         System.assertEquals(ptm.jstcl__User__c, acc.Vertical_Account_Manager__c);
        System.debug('>>>>>>>>> Ending Test');
        Test.stopTest();

   

    }
}


 
Raj VakatiRaj Vakati
try this
 
@isTest
private class addNAM_VAMtest{
    private static testMethod void testNAM_VAM(){
        Test.startTest();
        ADP_Department__c adp = new ADP_Department__c();
        adp.Name = 'Georgia';
        insert adp;
		
		  User u = new User(
 	    ProfileId = [SELECT Id FROM Profile WHERE Name = 'National Sales - Brooksource'].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'
		);

insert u ;  


         Contact con=new Contact(
            FirstName='fname',
            LastName = 'lname',
            Email = 'email@gmail.com',
            Phone = '9743800309'); 
        insert con; 
       
        ts2__Job__c job = new ts2__Job__c();
        job.Name = 'Software Engineer';
        insert job;
		
        Account acc = new Account(Name = 'Time Warner Inc.' ,National_Account_Manager__c  =u.Id , Vertical_Account_Manager__c    =u.Id);
        insert acc;
        
		ts2__HolidayCalendar__c hcal = new ts2__HolidayCalendar__c();
        hcal.Name= 'Holiday Calendar';
        insert hcal;
        
		Labor_Category__c lcat = new Labor_Category__c(Name='Software Engineer 1');  
        insert lcat;
        
		ts2__Placement__c plc = new ts2__Placement__c();
        plc.ts2__Job__c = job.Id;
        plc.Contractor_Type__c = 'Technical Youth';
        plc.Onboarding_Method__c= 'Staff Augmentation';
        plc.ts2__Start_Date__c= Date.newInstance(2019, 1, 7);
        plc.ts2__End_Date__c= Date.newInstance(2019, 2, 15);
        plc.jstcl__HireType__c= 'W2';
        plc.ADP_Department__c= adp.Id;
        plc.ts2__Bill_Rate__c= 60.00;
        plc.ts2__Pay_Rate__c= 30.00;
        plc.jstcl__Invoice_Batch__c= 'Placement';
        plc.ts2__Client__c= acc.Id;
        plc.ts2__Hiring_Manager__c= con.Id;
        plc.jstcl__TimecardApprover__c= con.Id;
        plc.jstcl__HolidayCalendar__c= hcal.Id;
		plc.State_Worked_In_Non_TFI_Placements__c= 'GA - Georgia';
        plc.jstcl__Overtime_Bill_Rate__c= 60;
        plc.jstcl__Burden__c= 0.20;
        plc.jstcl__Accounts_Receivable_U__c= '00537000005d7F2AAI';
        plc.jstcl__Terms__c= 'Net 60';
        plc.ts2__Employee__c= con.Id;
        plc.Labor_Category__c= lcat.Id;
        
        insert plc;

		
            
		
        jstcl__TG_Commission_Plan__c cp = new jstcl__TG_Commission_Plan__c(name = 'National Vertical Manager (NVM or NAM)');
        insert cp;
        
        
        
        System.debug('>>>>>>>>> Starting Test');
        jstcl__PlacementTeamMember__c ptm = new jstcl__PlacementTeamMember__c(jstcl__Placement__c = plc.Id,
                                                                              jstcl__User__c = u.Id,
                                                                    //          jstcl__CommissionPlan__c = cp.Id,
                                                                              jstcl__SplitPercent__c = 100);
        insert ptm;
        
        System.assertEquals(ptm.jstcl__User__c, acc.National_Account_Manager__c);
         System.assertEquals(ptm.jstcl__User__c, acc.Vertical_Account_Manager__c);
        System.debug('>>>>>>>>> Ending Test');
        Test.stopTest();

   

    }
}