+ Start a Discussion
Luke Higgins 22Luke Higgins 22 

Need help writing a simple test class

I can't seem to figure out how to get this test class working:

getPTM.apxc - 
public class getPTM {
@AuraEnabled
    public static List<jstcl__PlacementTeamMember__c> plcWizGetPTM(List<Id> plcIds) {
        List<jstcl__PlacementTeamMember__c> rlist= new List<jstcl__PlacementTeamMember__c>();
        rlist = [SELECT id,jstcl__Placement__r.Name,jstcl__User__r.Name,jstcl__CommissionPlan__r.Name,jstcl__SplitPercent__c 
                                                        FROM jstcl__PlacementTeamMember__c 
                                                        WHERE jstcl__Placement__r.Id IN :plcIds ];
        return rlist;
    }
}

Test class so far - 
@isTest
public class getPTMtest {
    static testMethod void testPTM(){
        jstcl__PlacementTeamMember__c testTeamMem = new jstcl__PlacementTeamMember__c();
        testTeamMem.jstcl__Placement__c = 'a211G000001IvdlQAC';
        testTeamMem.jstcl__User__c = '00537000003HbjPAAS';
        testTeamMem.jstcl__CommissionPlan__c = 'a4g370000008PGTAA2';
        testTeamMem.jstcl__SplitPercent__c = 100;
        testTeamMem.OwnerId = '00537000003HbjPAAS';
		
		  insert testTeamMem;
        
        Test.startTest();
          getPTM.plcWizGetPTM(testTeamMem, 'a211G000001IvdlQAC');
        Test.stopTest();
        
    }
}

Thanks
 
Best Answer chosen by Luke Higgins 22
Raj VakatiRaj Vakati
You know the issue is because you are passing the user id that may not exist so try like this 
 
@isTest
private class getPTMTest {
    
    static testmethod void testScheduledJob() {
        Test.startTest();
		
		ts2__Placement__c plc = new ts2__Placement__c();
		//places.Name ='Demo';
		 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;
        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;
		// add all required fields 
		insert plc ;
		
		
		jstcl__CommissionPlan__c comm = new jstcl__CommissionPlan__c();
		//add all reqiuired fields 
		insert comm ;
		 
		
		Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
		
		System.run
   
		jstcl__PlacementTeamMember__c testTeamMem = new jstcl__PlacementTeamMember__c();
			testTeamMem.jstcl__Placement__c = plc.Id ; 
     	    testTeamMem.jstcl__User__c = uu.Id;
    	    testTeamMem.jstcl__CommissionPlan__c = comm.Id;
     	    testTeamMem.jstcl__SplitPercent__c = 100;
    	    testTeamMem.jstcl__PlacementType__c = 'Test text';
            
//add all requried fields 
		insert testTeamMem ;
	getPTM.plcWizGetPTM(new List<Id>{plc.Id});
   
 	  Test.stopTest();
    }
}

 

All Answers

Raj VakatiRaj Vakati
try this
 
@isTest
private class getPTMTest {
    
    static testmethod void testScheduledJob() {
        Test.startTest();
		
		jstcl__Placement__c places = new jstcl__Placement__c();
		places.Name ='Demo';
		// add all required fields 
		insert places ;
   
jstcl__PlacementTeamMember__c members = new jstcl__PlacementTeamMember__c();
members.jstcl__Placement__c = places.Id ; 
//add all requried fields 
insert members ;
getPTM.plcWizGetPTM(new List<Id>{places.Id});
   
   Test.stopTest();
    }
}

 
Luke Higgins 22Luke Higgins 22
Hey Raj,

It seems like I am getting this error that happens on line 64 of the test class-
"System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, jstcl.PlacementTeamMember: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object"

Test class:
@isTest
private class getPTMTest {
    
    static testmethod void testScheduledJob() {
        Test.startTest();
		
		ts2__Placement__c plc = new ts2__Placement__c();
		//places.Name ='Demo';
		 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;
        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;
		// add all required fields 
		insert plc ;
   
		jstcl__PlacementTeamMember__c testTeamMem = new jstcl__PlacementTeamMember__c();
			testTeamMem.jstcl__Placement__c = plc.Id ; 
     	    testTeamMem.jstcl__User__c = '00537000003HbjPAAS';
    	    testTeamMem.jstcl__CommissionPlan__c = 'a4g370000008PGTAA2';
     	    testTeamMem.jstcl__SplitPercent__c = 100;
    	    testTeamMem.OwnerId = '00537000003HbjPAAS';
        	testTeamMem.CreatedById = '00537000003HbjPAAS';
            testTeamMem.LastModifiedById = '00537000003HbjPAAS';
            testTeamMem.jstcl__PlacementType__c = 'Test text';
            
//add all requried fields 
		insert testTeamMem ;
	getPTM.plcWizGetPTM(new List<Id>{plc.Id});
   
 	  Test.stopTest();
    }
}

If I had to guess, I think it is missing the "Name" field of the jstcl__PlacementTeamMember__c. This field is an auto number and it won't let me insert anything into it however.

Thank you for your help.
Raj VakatiRaj Vakati
You know the issue is because you are passing the user id that may not exist so try like this 
 
@isTest
private class getPTMTest {
    
    static testmethod void testScheduledJob() {
        Test.startTest();
		
		ts2__Placement__c plc = new ts2__Placement__c();
		//places.Name ='Demo';
		 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;
        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;
		// add all required fields 
		insert plc ;
		
		
		jstcl__CommissionPlan__c comm = new jstcl__CommissionPlan__c();
		//add all reqiuired fields 
		insert comm ;
		 
		
		Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
		
		System.run
   
		jstcl__PlacementTeamMember__c testTeamMem = new jstcl__PlacementTeamMember__c();
			testTeamMem.jstcl__Placement__c = plc.Id ; 
     	    testTeamMem.jstcl__User__c = uu.Id;
    	    testTeamMem.jstcl__CommissionPlan__c = comm.Id;
     	    testTeamMem.jstcl__SplitPercent__c = 100;
    	    testTeamMem.jstcl__PlacementType__c = 'Test text';
            
//add all requried fields 
		insert testTeamMem ;
	getPTM.plcWizGetPTM(new List<Id>{plc.Id});
   
 	  Test.stopTest();
    }
}

 
This was selected as the best answer