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
jeba mjeba m 

Test class for the follwing

Hi am new to salesforce please write the test class for the following controller.....

public class profileshortlists {

Id id = ApexPages.currentPage().getParameters().get('id');
public List<Profiles_Shortlisting__c> profilelists{get; set;}



public profileshortlists(ApexPages.StandardController controller) {
        profilelists = new List<Profiles_Shortlisting__c>([select name,Candidate__c,Position__c,Candidate_Status__c,resume__c from Profiles_Shortlisting__c where id =: id]);
       
        profilelists.add(new Profiles_Shortlisting__c(name='temp'));
    
    
        }
     public PageReference save() {
  insert profilelists;
  System.PageReference pageReference = new System.PageReference('/a02/o');
 
   return  PageReference;
   //return new PageReference('/'+id);
  }

}
Best Answer chosen by jeba m
Vatsal KothariVatsal Kothari
Hi Jeba,

You can refer below code:
@istest
public class ProfileShortListsTest{

	public static testmethod void testProfileListMethod ()
	{
		Position__c pos = new Position__c();
		pos.Name  = 'Position';
		pos.Job_Description__c = 'test';
		insert pos;
		
		Candidate__c can = new Candidate__c();
		can.First_Name__c = 'test'
		can.Last_Name__c = 'test';
		can.Email__c = 'test@test.com';
		insert can;
	
		Profiles_Shortlisting__c profiles = new Profiles_Shortlisting__c();
		profiles.name = 'Profile Name';
		profiles.Candidate__c = can.Id;
		profiles.Position__c = pos.Id;
		profiles.Candidate_Status__c = 'Open';
		profiles.resume__c = 'Test';
		insert profiles;
		
		PageReference pageRef = Page.ProfileListPage; //ProfileListPage with your vf page name
        Test.setCurrentPage(pageRef);
      
        ApexPages.currentPage().getParameters().put('id', profiles.Id);
		
		profileshortlists pro = new profileshortlists();
		pro.save();
		
	}
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal

All Answers

Vatsal KothariVatsal Kothari
Hi Jeba,

You can refer below code:
@istest
public class ProfileShortListsTest{

	public static testmethod void testProfileListMethod ()
	{
		Position__c pos = new Position__c();
		pos.Name  = 'Position';
		pos.Job_Description__c = 'test';
		insert pos;
		
		Candidate__c can = new Candidate__c();
		can.First_Name__c = 'test'
		can.Last_Name__c = 'test';
		can.Email__c = 'test@test.com';
		insert can;
	
		Profiles_Shortlisting__c profiles = new Profiles_Shortlisting__c();
		profiles.name = 'Profile Name';
		profiles.Candidate__c = can.Id;
		profiles.Position__c = pos.Id;
		profiles.Candidate_Status__c = 'Open';
		profiles.resume__c = 'Test';
		insert profiles;
		
		PageReference pageRef = Page.ProfileListPage; //ProfileListPage with your vf page name
        Test.setCurrentPage(pageRef);
      
        ApexPages.currentPage().getParameters().put('id', profiles.Id);
		
		profileshortlists pro = new profileshortlists();
		pro.save();
		
	}
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
This was selected as the best answer
jeba mjeba m
But it shows the following error Compile Error: Invalid type: Position__c at line 6 column 31
Vatsal KothariVatsal Kothari
Candidate__c & Position__c fields of Profiles_Shortlisting__c object are lookup?
jeba mjeba m
for profile_shortlisting,candidate__c and position__c are lookup objects......
jeba mjeba m
yes in profile_shortlisting__c object candidate__c and position__c fields are lookup
Vatsal KothariVatsal Kothari
Replace 6th and 11th line with Correct API name of the Object.
In the 6th line instead of Position__c use correct API name for Position Object and same for Candidate Object.
And insert mandatory fields of both objects.
jeba mjeba m
hmmm thanks thank you very much i will try......... thanks for ur response
jeba mjeba m
yeah i changed all the required things but it shows error as "unexpected token: 'insert' at line 18" in (insert can;)
Vatsal KothariVatsal Kothari
can you please share your updated code?
jeba mjeba m
@istest
public class ProfileShortListsTest{

    public static testmethod void testProfileListMethod ()
    {
        Job_Request__c pos = new Job_Request__c();
        pos.Name  = 'Job_Request';
        pos.Job_Description__c = 'test';
        pos.Hiring_Manager__c  = 'test';
        pos.Job_Max_Pay__c_c = 'test';
        pos.Job_Min_Pay__c_c = 'test';
        insert pos;
       
        Candidate__c can = new Candidate__c();
        can.Name = 'test';
        can.Country__c = 'test';
        can.Currently_Employed__c = 'test'
        can.Name = 'test';
        can.Country__c = 'test';
        can.Currently_Employed__c = 'test'
        insert can;
   
        Profiles_Shortlisting__c profiles = new Profiles_Shortlisting__c();
        profiles.name = 'Profile Name';
        profiles.Candidate__c = can.Id;
        profiles.Position__c = pos.Id;
        profiles.Candidate_Status__c = 'Open';
        profiles.resume__c = 'Test';
        insert profiles;
       
        PageReference pageRef = Page.Profileupdate  ; //ProfileListPage with your vf page name
        Test.setCurrentPage(pageRef);
     
        ApexPages.currentPage().getParameters().put('id', profiles.Id);
       
        profileshortlists pro = new profileshortlists();
        pro.save();
       
    }
}


Vatsal KothariVatsal Kothari
You missed semicolon in line 17
jeba mjeba m
now it shows illegal assignment from string to decimal....(or: Illegal assignment from String to Decimal at line 10 column 9)
@istest
public class ProfileShortListsTest{

    public static testmethod void testProfileListMethod ()
    {
        Job_Request__c pos = new Job_Request__c();
        pos.Name  = 'Job_Request';
        pos.Job_Description__c = 'test';
        pos.Hiring_Manager__c  = 'test';
        pos.Max_Pay__c = 'test';
        pos.Min_Pay__c = 'test';
        insert pos;
       
        Candidate__c can = new Candidate__c();
        can.Name = 'test';
        can.Country__c = 'test';
        can.Currently_Employed__c = 'test';
        insert can;
   
        Profiles_Shortlisting__c profiles = new Profiles_Shortlisting__c();
        profiles.name = 'Profile Name';
        profiles.Candidate__c = can.Id;
        profiles.Position__c = pos.Id;
        profiles.Candidate_Status__c = 'Open';
        profiles.resume__c = 'Test';
        insert profiles;
       
        PageReference pageRef = Page.Profileupdate  ; //ProfileListPage with your vf page name
        Test.setCurrentPage(pageRef);
     
        ApexPages.currentPage().getParameters().put('id', profiles.Id);
       
        profileshortlists pro = new profileshortlists();
        pro.save();
       
    }
}
jeba mjeba m
pos.Max_Pay__c = 'test';
 pos.Min_Pay__c = 'test'; are number fields....
Vatsal KothariVatsal Kothari
Update 10th 11th and 17th line.
pos.Job_Max_Pay__c_c = 20000.0 ;
pos.Job_Min_Pay__c_c = 5000.0 ;

If Currently_Employed__c is checkbox, than update it with
can.Currently_Employed__c = true;
jeba mjeba m
but it still shows the same error....
jeba mjeba m
it shows the same following error(Error: Compile Error: Illegal assignment from String to Decimal at line 10 column 9)..........

@istest
public class ProfileShortListsTest{

    public static testmethod void testProfileListMethod ()
    {
        Job_Request__c pos = new Job_Request__c();
        pos.Name  = 'Job_Request';
        pos.Job_Description__c = 'test';
        pos.Hiring_Manager__c  = 'test';
        pos.Max_Pay__c = '20000';
        pos.Min_Pay__c = '5000';
        insert pos;
       
        Candidate__c can = new Candidate__c();
        can.Name = 'test';
        can.Country__c = 'test';
        can.Currently_Employed__c = 'true';
        insert can;
   
        Profiles_Shortlisting__c profiles = new Profiles_Shortlisting__c();
        profiles.name = 'Profile Name';
        profiles.Candidate__c = can.Id;
        profiles.Position__c = pos.Id;
        profiles.Candidate_Status__c = 'Open';
       // profiles.resume__c = 'Test';
        insert profiles;
       
        PageReference pageRef = Page.Profileupdate  ; //ProfileListPage with your vf page name
        Test.setCurrentPage(pageRef);
     
        ApexPages.currentPage().getParameters().put('id', profiles.Id);
       
        profileshortlists pro = new profileshortlists();
        pro.save();
       
    }
}
jeba mjeba m
pos.Job_Description__c = 'test'; is lookup field to user.....
Vatsal KothariVatsal Kothari
Updated code:

@istest
public class ProfileShortListsTest{

    public static testmethod void testProfileListMethod ()
    {
		Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
		
		User u = new User(Alias = 'standt', Email='standarduser123@testorg.com', 
		EmailEncodingKey='UTF-8', LastName='Testing123', LanguageLocaleKey='en_US', 
		LocaleSidKey='en_US', ProfileId = p.Id, 
		TimeZoneSidKey='America/Los_Angeles', UserName='standarduser123@testorg123.com');
	
        Job_Request__c pos = new Job_Request__c();
        pos.Name  = 'Job_Request';
        pos.Job_Description__c = u.id;
        pos.Hiring_Manager__c  = 'test';
        pos.Job_Max_Pay__c_c = 20000.5;
        pos.Job_Min_Pay__c_c = 5000.5;
        insert pos;
       
        Candidate__c can = new Candidate__c();
        can.Name = 'test';
        can.Country__c = 'test';
        can.Currently_Employed__c = true;
        insert can;
   
        Profiles_Shortlisting__c profiles = new Profiles_Shortlisting__c();
        profiles.name = 'Profile Name';
        profiles.Candidate__c = can.Id;
        profiles.Position__c = pos.Id;
        profiles.Candidate_Status__c = 'Open';
        profiles.resume__c = 'Test';
        insert profiles;
       
        PageReference pageRef = Page.Profileupdate  ; //ProfileListPage with your vf page name
        Test.setCurrentPage(pageRef);
     
        ApexPages.currentPage().getParameters().put('id', profiles.Id);
       
        profileshortlists pro = new profileshortlists();
        pro.save();
       
    }
}


jeba mjeba m
yeah thanks for your patience........ but it shows the following error(Constructor not defined: [profileshortlists].<Constructor>() at line 33 column 33) please help me....
Vatsal KothariVatsal Kothari
replace code below 27th line with
Profiles_Shortlisting__c pro = new Profiles_Shortlisting__c();
        pro.name = 'Profile Name';
        pro.Candidate__c = can.Id;
        pro.Position__c = pos.Id;
        pro.Candidate_Status__c = 'Open';
        pro.resume__c = 'Test';
        insert pro;
       
        PageReference pageRef = Page.Profileupdate  ; //ProfileListPage with your vf page name
        Test.setCurrentPage(pageRef);
     
        ApexPages.currentPage().getParameters().put('id', pro.Id);
       
        profileshortlists pro = new profileshortlists();
        pro.save();
       
    }
}



jeba mjeba m
but it still shows the same error.... :(       (Constructor not defined: [profileshortlists].<Constructor>() at line 33 column 32)
Vatsal KothariVatsal Kothari
Updated code:

@istest
public class ProfileShortListsTest{

    public static testmethod void testProfileListMethod ()
    {
		Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
		
		User u = new User(Alias = 'standt', Email='standarduser123@testorg.com', 
		EmailEncodingKey='UTF-8', LastName='Testing123', LanguageLocaleKey='en_US', 
		LocaleSidKey='en_US', ProfileId = p.Id, 
		TimeZoneSidKey='America/Los_Angeles', UserName='standarduser123@testorg123.com');
	
        Job_Request__c pos = new Job_Request__c();
        pos.Name  = 'Job_Request';
        pos.Job_Description__c = u.id;
        pos.Hiring_Manager__c  = 'test';
        pos.Job_Max_Pay__c_c = 20000.5;
        pos.Job_Min_Pay__c_c = 5000.5;
        insert pos;
       
        Candidate__c can = new Candidate__c();
        can.Name = 'test';
        can.Country__c = 'test';
        can.Currently_Employed__c = 'test';
        insert can;
   
		
        Profiles_Shortlisting__c pro = new Profiles_Shortlisting__c();
        pro.name = 'Profile Name';
        pro.Candidate__c = can.Id;
        pro.Position__c = pos.Id;
        pro.Candidate_Status__c = 'Open';
        pro.resume__c = 'Test';
        insert pro;
        
		ApexPages.StandardController sc = new ApexPages.StandardController(pro);
		profileshortlists pro = new profileshortlists(sc);
        PageReference pageRef = Page.Profileupdate  ; //ProfileListPage with your vf page name
        Test.setCurrentPage(pageRef);
     
        ApexPages.currentPage().getParameters().put('id', pro.Id);       
        pro.save();
       
    }
}