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
VRK1VRK1 

Test coverage failed for Lookup fields in Apex class

Hi ,
for below code, Test coverage is failed and getting erros. can you pls check below my post :

Apex classs :
User-added imageand below is my Test class :
@isTest
public class Test_PickListController {
    static testMethod void insertValues() {
        Resourse__c resource = new Resourse__c();
        resource.Name='Test1';
        resource.Job_type__c='Automation Engineer';
        resource.Type_of__c= 'Bestshore';
        resource.EngagementType__c = 'Fixed Price';
        insert resource;
        
        PickListController.getResource(resource.Id);
        
        Skill_Management__c skillMgt = new Skill_Management__c();
        skillMgt.Name='Java' ;
        skillMgt.Skill_Category__c='Java' ;
        skillMgt.Skills__c='Java' ;
        insert skillMgt;
        
        Skill_Management__c skillUpdate = [select Id,Name,Skill_Category__c,Skills__c from Skill_Management__c
                                           where Id =:skillMgt.Id limit 1];
              
        Skill__c ac = new Skill__c();
       /* ac.Resource__c = resource.Id;
        ac.Skill_Management__c = skillUpdate.id ;        
        insert ac;*/

        PickListController pc = new PickListController();
        
        PickListController.insertValues(ac,skillMgt.Id,resource.Id);
          
    }
}

When i try to execute the Test class getting below and its coverage only 35% .
Class.PickListController.insertValues: line 29, column 1
Class.Test_PickListController.insertValues: line 29, column 1.


can
 you pls check and help on this.
Thanks

Best Answer chosen by VRK1
CharuDuttCharuDutt
Hii VRK
Try Below Code
@isTest
public class Test_PickListController {
	@isTest
    public static void unitTest() {
        Resourse__c resource = new Resourse__c();
        resource.Name='Test1';
        resource.Job_type__c='Automation Engineer';
        resource.Type_of__c= 'Bestshore';
        resource.EngagementType__c = 'Fixed Price';
        insert resource;
        
        Skill_Management__c skillMgt = new Skill_Management__c();
        skillMgt.Name='Java' ;
        skillMgt.Skill_Category__c='Java' ;
        skillMgt.Skills__c='Java' ;
        insert skillMgt;
        
              
        Skill__c ac = new Skill__c();
		
		
		PickListController.getResource(resource.Id);
        PickListController pc = new PickListController();
        PickListController.insertValues(ac,resource.Id,skillMgt.Id);
          
    }
}

 

All Answers

CharuDuttCharuDutt
Hii VRK
Try Below Test Class I've Made Changes
@isTest
public class Test_PickListController {
	@isTest
    public static void unitTest() {
        Resourse__c resource = new Resourse__c();
        resource.Name='Test1';
        resource.Job_type__c='Automation Engineer';
        resource.Type_of__c= 'Bestshore';
        resource.EngagementType__c = 'Fixed Price';
        insert resource;
        
        Skill_Management__c skillMgt = new Skill_Management__c();
        skillMgt.Name='Java' ;
        skillMgt.Skill_Category__c='Java' ;
        skillMgt.Skills__c='Java' ;
        insert skillMgt;
        
              
        Skill__c ac = new Skill__c();
        ac.Resource__c = resource.Id;
        ac.Skill_Management__c = skillMgt.id ;        
        insert ac;
		
		
		PickListController.getResource(resource.Id);
        PickListController pc = new PickListController();
        PickListController.insertValues(ac,resource.Id,skillMgt.Id);
          
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
VRK1VRK1
Thank you Charu for your prmopt response.
When i try to exeucte the test class getting below error :
System.SObjectException: Field is not writeable: Skill__c.Resource__c
---
Class.PickListController.insertValues: line 33, column 1
Class.Test_PickListController.unitTest: line 26, column 1


@isTest
public class Test_PickListController {
    @isTest
    public static void unitTest() {
        Resourse__c resource = new Resourse__c();
        resource.Name='Test1';
        resource.Job_type__c='Automation Engineer';
        resource.Type_of__c= 'Bestshore';
        resource.EngagementType__c = 'Fixed Price';
        insert resource;
        
        Skill_Management__c skillMgt = new Skill_Management__c();
        skillMgt.Name='Java' ;
        skillMgt.Skill_Category__c='Java' ;
        skillMgt.Skills__c='Java' ;
        insert skillMgt;
        
                    
        Skill__c ac = new Skill__c();
        ac.Resource__c = resource;                     // Here getting error . This is lookup field 
        ac.Skill_Management__c = skillMgt.id ;        
        insert ac;
        
        PickListController.getResource(resource.Id);
        PickListController pc = new PickListController();
        PickListController.insertValues(ac,resource.Id,skillMgt.Id);
          
    }
}

Any ideas, how to fix this ....Thanks in advance
CharuDuttCharuDutt
Hii VRK 
Comment That Line
 
VRK1VRK1
Yes ealier i already tried by comment that line ...but getting below error :
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Resource__c]: [Resource__c]

Any clues do you have ?
CharuDuttCharuDutt
Hii VRK
Try Below Code
@isTest
public class Test_PickListController {
	@isTest
    public static void unitTest() {
        Resourse__c resource = new Resourse__c();
        resource.Name='Test1';
        resource.Job_type__c='Automation Engineer';
        resource.Type_of__c= 'Bestshore';
        resource.EngagementType__c = 'Fixed Price';
        insert resource;
        
        Skill_Management__c skillMgt = new Skill_Management__c();
        skillMgt.Name='Java' ;
        skillMgt.Skill_Category__c='Java' ;
        skillMgt.Skills__c='Java' ;
        insert skillMgt;
        
              
        Skill__c ac = new Skill__c();
		
		
		PickListController.getResource(resource.Id);
        PickListController pc = new PickListController();
        PickListController.insertValues(ac,resource.Id,skillMgt.Id);
          
    }
}

 
This was selected as the best answer
VRK1VRK1
Thank you ........now code is working.
I understand ,for lookup no need to declare right..
Thank you