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
DolgoldyDolgoldy 

Want help here

Hi all, help me to write test class
public class myjob 
{
@AuraEnabled(cacheable=true)
    Public static List<Job_Requirement__c> myservice()
    {
        list<Job_Requirement__c> lstjob = [select id, Name, Service_Name__c, Type__c from Job_Requirement__c where
                                          Type__c='Standard'];
        return lstjob;
    }

@AuraEnabled(cacheable=true)
    Public static List<Job_Requirement__c> myservicegeneral()
    {
        list<Job_Requirement__c> lstjobgeneral = [select id, Name, Service_Name__c, Type__c from Job_Requirement__c where
                                          Type__c='Specialty'];
        return lstjobgeneral;
    }
    @AuraEnabled(cacheable=true)
    Public static List<Job_Requirement__c> myservicinteriror()
    {
        list<Job_Requirement__c> lstjobinteriror = [select id, Name, Service_Name__c, Type__c from Job_Requirement__c where
                                          Type__c='Custom Named'];
        return lstjobinteriror;
    }

    @AuraEnabled(cacheable=false)
    Public static List<Job_Requirement__c> getStandardWorkOrders()
    {
        list<Job_Requirement__c> lstjob = [select id, Name, Service_Name__c, Type__c from Job_Requirement__c ];
                                          //Type__c='Standard'];
        return lstjob;
    }

    Public static List<Job_Requirement__c> getSpecialityWokOrders()
    {
        list<Job_Requirement__c> lstjobgeneral = [select id, Name, Service_Name__c, Type__c from Job_Requirement__c where
                                          Type__c='Specialty'];
        return lstjobgeneral;
    }
}
Best Answer chosen by Dolgoldy
mukesh guptamukesh gupta
Hi Dolgoldy,

Please use below code:-
 
@isTest
private class MyjobTest{
    @testSetup
    static void setupTestData(){
        
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        User usr=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = 'mk@test.org', 
                         Username = 'mk@test123.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert usr;

        Job_Requirement__c jobObj = new Job_Requirement__c(Name = 'Test job Standard', Service_Name__c = 'test', Type__c = 'Standard');
        Insert jobStandard; 
        Job_Requirement__c jobObj = new Job_Requirement__c(Name = 'Test job Specialty', Service_Name__c = 'test', Type__c = 'Specialty');
        Insert jobStandard; 
        Job_Requirement__c jobObj = new Job_Requirement__c(Name = 'Test job Custom Named', Service_Name__c = 'test', Type__c = 'Custom Named');
        Insert jobStandard; 
        
    }
    static testMethod void test_getJob(){
        System.Test.startTest();
        Job_Requirement__c jobStandardObj = [select id, Name, Service_Name__c, Type__c from Job_Requirement__c where
                                          Type__c='Standard'];
        Job_Requirement__c jobSpecialtyObj = [select id, Name, Service_Name__c, Type__c from Job_Requirement__c where
                                          Type__c='Specialty'];
        Job_Requirement__c jobCustomNamedObj = [select id, Name, Service_Name__c, Type__c from Job_Requirement__c where
                                          Type__c='Custom Named'];
        myjob.myservice();
        myjob.myservicegeneral();
        myjob.myservicinteriror();
        myjob.getStandardWorkOrders();
        myjob.getSpecialityWokOrders();
        System.test.stopTest();
    }
    
}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

All Answers

mukesh guptamukesh gupta
Hi Dolgoldy,

Please use below code:-
 
@isTest
private class MyjobTest{
    @testSetup
    static void setupTestData(){
        
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        User usr=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = 'mk@test.org', 
                         Username = 'mk@test123.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert usr;

        Job_Requirement__c jobObj = new Job_Requirement__c(Name = 'Test job Standard', Service_Name__c = 'test', Type__c = 'Standard');
        Insert jobStandard; 
        Job_Requirement__c jobObj = new Job_Requirement__c(Name = 'Test job Specialty', Service_Name__c = 'test', Type__c = 'Specialty');
        Insert jobStandard; 
        Job_Requirement__c jobObj = new Job_Requirement__c(Name = 'Test job Custom Named', Service_Name__c = 'test', Type__c = 'Custom Named');
        Insert jobStandard; 
        
    }
    static testMethod void test_getJob(){
        System.Test.startTest();
        Job_Requirement__c jobStandardObj = [select id, Name, Service_Name__c, Type__c from Job_Requirement__c where
                                          Type__c='Standard'];
        Job_Requirement__c jobSpecialtyObj = [select id, Name, Service_Name__c, Type__c from Job_Requirement__c where
                                          Type__c='Specialty'];
        Job_Requirement__c jobCustomNamedObj = [select id, Name, Service_Name__c, Type__c from Job_Requirement__c where
                                          Type__c='Custom Named'];
        myjob.myservice();
        myjob.myservicegeneral();
        myjob.myservicinteriror();
        myjob.getStandardWorkOrders();
        myjob.getSpecialityWokOrders();
        System.test.stopTest();
    }
    
}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
This was selected as the best answer
DolgoldyDolgoldy
HI mukesh 
Thank u this help me lot

can u help me in this getting error as Variable does not exist: id

@isTest
public class GetProjectJobRequirementJunctionTest {
    

@isTest 
    
    static void setupTest1(){ 
        
ProjectJobRequirementJunction__c objJob1 = new ProjectJobRequirementJunction__c();
objJob1.name = 'test'; 
//objJob1.Work_Name__c = 'Test service'; 
objJob1.Schedule_Status__c = 'standard'; 
objjob1.Project__c='test project';
insert objjob1; 

Test.startTest(); 
         list<ProjectJobRequirementJunction__c> opId = [SELECT Id,name FROM ProjectJobRequirementJunction__c ];
        
       // mstResult = RFPDateController.fetchDates(opId);

GetProjectJobRequirementJunction_ctlr.getRecords(opid.id); 
GetProjectJobRequirementJunction_ctlr.fetchRecords(opid.id);       

Test.stopTest(); 

    @isTest 
    
  static void setupTest2(){ 
      
    Contractor__c objcon=new Contractor__c();
      objcon.name='test1';
      objcon.Link__c='test contractor';
      
    Test.startTest(); 
         
list<  Contractor__c> opId = [SELECT Id,name FROM   Contractor__c ];
GetProjectJobRequirementJunction_ctlr.getContractorRecords(); 
GetProjectJobRequirementJunction_ctlr.getLinkFromContractorRecords(opid.Id);      

Test.stopTest(); 
    
}
     @isTest 
    
  static void setupTest3(){ 
      
   Project_Work_Order_Line_Item__c objcon=new Project_Work_Order_Line_Item__c();
     // objcon.name='test1';
      objcon.QTY__c=101;
      objcon.Piece_Rate_QTY__c=100;
     objcon.Materials_QTY__c =102;
        objcon.Rate__c=5;
         objcon.Hourly__c=true;
          
    Test.startTest(); 
         
list<  Project_Work_Order_Line_Item__c> opId = [SELECT Id,name FROM   Project_Work_Order_Line_Item__c ];
GetProjectJobRequirementJunction_ctlr.getLineItemsold(opid.id); 
GetProjectJobRequirementJunction_ctlr.getLineItems(opid.Id);      

Test.stopTest(); 
    
}
}

 
mukesh guptamukesh gupta
Hi Dolgoldy,

Please use below code:-
 
@isTest
public class GetProjectJobRequirementJunctionTest {
    @isTest static void setupTest1(){ 
        
ProjectJobRequirementJunction__c objJob1 = new ProjectJobRequirementJunction__c();
objJob1.name = 'test'; 
//objJob1.Work_Name__c = 'Test service'; 
objJob1.Schedule_Status__c = 'standard'; 
objjob1.Project__c='test project';
insert objjob1; 

Test.startTest(); 
         list<ProjectJobRequirementJunction__c> opId = [SELECT Id,name FROM ProjectJobRequirementJunction__c ];
        
       // mstResult = RFPDateController.fetchDates(opid[0]);

GetProjectJobRequirementJunction_ctlr.getRecords(opid[0].id); 
GetProjectJobRequirementJunction_ctlr.fetchRecords(opid[0].id);       

Test.stopTest(); 
} 
    @isTest 
    
  static void setupTest2(){ 
      
    Contractor__c objcon=new Contractor__c();
      objcon.name='test1';
      objcon.Link__c='test contractor';
      insert objcon;
    Test.startTest(); 
         
list<Contractor__c> opId = [SELECT Id,name FROM   Contractor__c ];
GetProjectJobRequirementJunction_ctlr.getContractorRecords(); 
GetProjectJobRequirementJunction_ctlr.getLinkFromContractorRecords(opid[0].Id);      

Test.stopTest(); 
    
}
     @isTest 
    
  static void setupTest3(){ 
      
   Project_Work_Order_Line_Item__c objcon=new Project_Work_Order_Line_Item__c();
     // objcon.name='test1';
      objcon.QTY__c=101;
      objcon.Piece_Rate_QTY__c=100;
     objcon.Materials_QTY__c =102;
        objcon.Rate__c=5;
         objcon.Hourly__c=true;
        insert objcon;  
    Test.startTest(); 
         
list<  Project_Work_Order_Line_Item__c> opId = [SELECT Id,name FROM   Project_Work_Order_Line_Item__c ];
GetProjectJobRequirementJunction_ctlr.getLineItemsold(opid[0].id); 
GetProjectJobRequirementJunction_ctlr.getLineItems(opid[0].Id);      

Test.stopTest(); 
    
}
}


Regards
Mukesh