You need to sign in to do that
Don't have an account?

TestClass
Hi,
How to write test class for below class.
public with sharing class JSS_JobSearch{
public Integer pageSize = 200;
public Integer pageNumber = 0;
public String Id {get; set;}
public Opportunity opportunityID {get; set;}
public List<Job_Search__c> jobSearchB {get;set;}
public List<Job_Search__c> jobSearchA {get;set;}
public List<Open_Job__c> OpenJobsTop{get;set;}
public List<Open_Job__c> OpenJobsTop2{get;set;}
public Job_Search__c jobsearch{get; set;}
public Map<String,Boolean> JobMap = new Map<String,Boolean>();
public List<Job_Search__c> OpenJobstest{get;set;}
public Boolean nextBool {get;set;}
public Boolean prevBool {get;set;}
public integer Openjobscount {get;set;}
public integer Openjobscount2 {get;set;}
public integer jobSearchcount {get;set;}
public String openjobsId {get; set;}
public integer count{get;set;}
public List<Open_Job__c> OpenJobsList{get;set;}
public String jSSSubIndustry{get;set;}
public JSS_JobSearch(){
This.id = Apexpages.currentpage().getparameters().get('id');
}
public void search(){
jSSSubIndustry ='';
OpportunityID = [Select id,name,JSS_Countries__c,JSS_Industry__c,Sub_Industry__c from Opportunity where id =:id];
Boolean baddata = False;
Boolean Sponsors = False;
jobSearchA = new List<Job_Search__c>();
jobSearchB = new List<Job_Search__c>();
String jSSCountry = opportunityID.JSS_Countries__c;
String jSSIndustry = opportunityID.JSS_Industry__c;
jSSSubIndustry = opportunityID.Sub_Industry__c;
String baseQuery='Select id,name,Check__c,Email_Send__c ,Email_Bounced_Back__c,LastModifiedBy.LastName,Dummy_Job_Link__c,LastModifiedDate,Sub_Industry_oj__c,Country__c,Expiry_Date__c,Occupation__r.name,Job_captured_date__c,Job_Link__c,Province__c,Industry__c,CreatedBy.Name from Open_Job__c where ';
if(jSSSubIndustry != null)
{
system.debug('has value in sub'+jSSSubIndustry);
baseQuery +='Country__c =: jSSCountry and Industry__c =: jSsIndustry and Sub_Industry_oj__c =: jSSSubIndustry and Email_Bounced_Back__c =:baddata and Expiry_Date__c >= Today order by Expiry_Date__c ASC';
}else
{
system.debug('does not have value in sub');
baseQuery +='Country__c =: jSSCountry and Industry__c =: jSsIndustry and Email_Bounced_Back__c =:baddata and Expiry_Date__c >= Today order by Expiry_Date__c ASC';
}
jobSearchB = [Select id,name,Country__c,Industry__c,Dummy_Job_Link_JS__c,LastModifiedBy.LastName,LastModifiedDate,Job_Link__c,Occupation__r.name,Opportunity__c,Province__c from Job_Search__c where Opportunity__c =: Id order by CreatedDate ASC];
Boolean baddatas = True;
OpenJobsList = [Select id,name,Check__c,Email_Send__c ,LastModifiedBy.LastName,LastModifiedDate,Country__c,Expiry_Date__c,Occupation__r.name,Job_captured_date__c,Job_Link__c,Province__c,Industry__c,Sub_Industry_oj__c,CreatedBy.Name from Open_Job__c where Country__c =: jSSCountry and Industry__c =: jSsIndustry and Sub_Industry_oj__c =: jSSSubIndustry and Expiry_Date__c >= Today order by Expiry_Date__c ASC];
Integer offset = pageNumber * pageSize;
String query = baseQuery + ' LIMIT '+pageSize +' OFFSET '+ offset;
String query2 = baseQuery;
OpenJobsTop = Database.query(query);
OpenJobsTop2 = Database.query(query);
Openjobscount = OpenJobsList.Size();
Openjobscount2= OpenJobsTop2.Size();
jobSearchcount = jobSearchB.size();
for(Open_Job__c opens: OpenJobsTop){
for(Job_Search__c jobs: jobSearchB){
//jobs.Dummy_Job_Link_JS__c = opens.Dummy_Job_Link__c;
system.debug('!!!!!!!!!!!!!!!!'+jobs.Dummy_Job_Link_JS__c);
if(opens.Dummy_Job_Link__c == jobs.Dummy_Job_Link_JS__c){
system.debug('@@@@@@@@@@@@@@@'+opens.name);
system.debug('@@@@@@@@@@@@@@@'+jobs.Name);
system.debug('0000000000000'+opens.Dummy_Job_Link__c);
system.debug('0000000000000'+jobs.Dummy_Job_Link_JS__c);
opens.check__c = True;
}
}
}
OpenJobstest = jobSearchB;
}
public Pagereference commits(){
jobSearchA = new List<Job_Search__c>();
for(Job_Search__c jobss:jobSearchB){
JobMap.put(jobss.Dummy_Job_Link_JS__c,True);
}
for(Open_Job__c FinalJobs: OpenJobsTop){
jobsearch = new Job_Search__c();
if(JobMap.get(FinalJobs.Dummy_Job_Link__c) != True){
if(FinalJobs.check__c == True){
jobSearch.Name = FinalJobs.Name;
jobSearch.Country__c = FinalJobs.Country__c;
jobSearch.Industry__c = FinalJobs.Industry__c;
if(jSSSubIndustry != null)
{
jobSearch.Sub_Industry_jss__c = FinalJobs.Sub_Industry_oj__c;
}
jobSearch.Job_Link__c = FinalJobs.Job_Link__c;
jobSearch.Dummy_Job_Link_JS__c = FinalJobs.Dummy_Job_Link__c;
jobSearch.Occupation__c = FinalJobs.Occupation__c;
jobSearch.Province__c = FinalJobs.Province__c;
jobSearch.Record_Id__c = FinalJobs.id +' '+ opportunityID.id;
jobSearch.opportunity__c = opportunityID.id;
jobSearch.Status__c = 'Completed';
jobSearchA.add(jobSearch);
jobSearchB.add(jobSearch);
}
}
}
if(jobSearchA.Size()>0)
insert jobSearchA;
PageReference customPage = new PageReference('/'+opportunityID.Id);
return customPage;
return null;
}
//Custom Pagination for next
public PageReference next(){
pageNumber++;
search();
prevBool = false;
return null;
}
//Custom Pagination for previous
public PageReference previous(){
pageNumber--;
if (pageNumber < 0)
return null;
search();
nextBool = false;
return null;
}
public void DeleteAcc(){
system.debug('ddddddddddddd');
if ( openjobsId == null ){
return;
}
Open_Job__c tobeDeleted = null;
for( Open_Job__c a : OpenJobsTop )
if ( a.Id == openjobsId ) {
tobeDeleted = a;
system.debug('tobeDeleted tobeDeleted '+tobeDeleted );
break;
}
if ( tobeDeleted != null ) {
tobeDeleted.Email_Bounced_Back__c= True;
system.debug('tobeDeleted'+tobeDeleted);
Update tobeDeleted;
}
}
}
Thanks.
How to write test class for below class.
public with sharing class JSS_JobSearch{
public Integer pageSize = 200;
public Integer pageNumber = 0;
public String Id {get; set;}
public Opportunity opportunityID {get; set;}
public List<Job_Search__c> jobSearchB {get;set;}
public List<Job_Search__c> jobSearchA {get;set;}
public List<Open_Job__c> OpenJobsTop{get;set;}
public List<Open_Job__c> OpenJobsTop2{get;set;}
public Job_Search__c jobsearch{get; set;}
public Map<String,Boolean> JobMap = new Map<String,Boolean>();
public List<Job_Search__c> OpenJobstest{get;set;}
public Boolean nextBool {get;set;}
public Boolean prevBool {get;set;}
public integer Openjobscount {get;set;}
public integer Openjobscount2 {get;set;}
public integer jobSearchcount {get;set;}
public String openjobsId {get; set;}
public integer count{get;set;}
public List<Open_Job__c> OpenJobsList{get;set;}
public String jSSSubIndustry{get;set;}
public JSS_JobSearch(){
This.id = Apexpages.currentpage().getparameters().get('id');
}
public void search(){
jSSSubIndustry ='';
OpportunityID = [Select id,name,JSS_Countries__c,JSS_Industry__c,Sub_Industry__c from Opportunity where id =:id];
Boolean baddata = False;
Boolean Sponsors = False;
jobSearchA = new List<Job_Search__c>();
jobSearchB = new List<Job_Search__c>();
String jSSCountry = opportunityID.JSS_Countries__c;
String jSSIndustry = opportunityID.JSS_Industry__c;
jSSSubIndustry = opportunityID.Sub_Industry__c;
String baseQuery='Select id,name,Check__c,Email_Send__c ,Email_Bounced_Back__c,LastModifiedBy.LastName,Dummy_Job_Link__c,LastModifiedDate,Sub_Industry_oj__c,Country__c,Expiry_Date__c,Occupation__r.name,Job_captured_date__c,Job_Link__c,Province__c,Industry__c,CreatedBy.Name from Open_Job__c where ';
if(jSSSubIndustry != null)
{
system.debug('has value in sub'+jSSSubIndustry);
baseQuery +='Country__c =: jSSCountry and Industry__c =: jSsIndustry and Sub_Industry_oj__c =: jSSSubIndustry and Email_Bounced_Back__c =:baddata and Expiry_Date__c >= Today order by Expiry_Date__c ASC';
}else
{
system.debug('does not have value in sub');
baseQuery +='Country__c =: jSSCountry and Industry__c =: jSsIndustry and Email_Bounced_Back__c =:baddata and Expiry_Date__c >= Today order by Expiry_Date__c ASC';
}
jobSearchB = [Select id,name,Country__c,Industry__c,Dummy_Job_Link_JS__c,LastModifiedBy.LastName,LastModifiedDate,Job_Link__c,Occupation__r.name,Opportunity__c,Province__c from Job_Search__c where Opportunity__c =: Id order by CreatedDate ASC];
Boolean baddatas = True;
OpenJobsList = [Select id,name,Check__c,Email_Send__c ,LastModifiedBy.LastName,LastModifiedDate,Country__c,Expiry_Date__c,Occupation__r.name,Job_captured_date__c,Job_Link__c,Province__c,Industry__c,Sub_Industry_oj__c,CreatedBy.Name from Open_Job__c where Country__c =: jSSCountry and Industry__c =: jSsIndustry and Sub_Industry_oj__c =: jSSSubIndustry and Expiry_Date__c >= Today order by Expiry_Date__c ASC];
Integer offset = pageNumber * pageSize;
String query = baseQuery + ' LIMIT '+pageSize +' OFFSET '+ offset;
String query2 = baseQuery;
OpenJobsTop = Database.query(query);
OpenJobsTop2 = Database.query(query);
Openjobscount = OpenJobsList.Size();
Openjobscount2= OpenJobsTop2.Size();
jobSearchcount = jobSearchB.size();
for(Open_Job__c opens: OpenJobsTop){
for(Job_Search__c jobs: jobSearchB){
//jobs.Dummy_Job_Link_JS__c = opens.Dummy_Job_Link__c;
system.debug('!!!!!!!!!!!!!!!!'+jobs.Dummy_Job_Link_JS__c);
if(opens.Dummy_Job_Link__c == jobs.Dummy_Job_Link_JS__c){
system.debug('@@@@@@@@@@@@@@@'+opens.name);
system.debug('@@@@@@@@@@@@@@@'+jobs.Name);
system.debug('0000000000000'+opens.Dummy_Job_Link__c);
system.debug('0000000000000'+jobs.Dummy_Job_Link_JS__c);
opens.check__c = True;
}
}
}
OpenJobstest = jobSearchB;
}
public Pagereference commits(){
jobSearchA = new List<Job_Search__c>();
for(Job_Search__c jobss:jobSearchB){
JobMap.put(jobss.Dummy_Job_Link_JS__c,True);
}
for(Open_Job__c FinalJobs: OpenJobsTop){
jobsearch = new Job_Search__c();
if(JobMap.get(FinalJobs.Dummy_Job_Link__c) != True){
if(FinalJobs.check__c == True){
jobSearch.Name = FinalJobs.Name;
jobSearch.Country__c = FinalJobs.Country__c;
jobSearch.Industry__c = FinalJobs.Industry__c;
if(jSSSubIndustry != null)
{
jobSearch.Sub_Industry_jss__c = FinalJobs.Sub_Industry_oj__c;
}
jobSearch.Job_Link__c = FinalJobs.Job_Link__c;
jobSearch.Dummy_Job_Link_JS__c = FinalJobs.Dummy_Job_Link__c;
jobSearch.Occupation__c = FinalJobs.Occupation__c;
jobSearch.Province__c = FinalJobs.Province__c;
jobSearch.Record_Id__c = FinalJobs.id +' '+ opportunityID.id;
jobSearch.opportunity__c = opportunityID.id;
jobSearch.Status__c = 'Completed';
jobSearchA.add(jobSearch);
jobSearchB.add(jobSearch);
}
}
}
if(jobSearchA.Size()>0)
insert jobSearchA;
PageReference customPage = new PageReference('/'+opportunityID.Id);
return customPage;
return null;
}
//Custom Pagination for next
public PageReference next(){
pageNumber++;
search();
prevBool = false;
return null;
}
//Custom Pagination for previous
public PageReference previous(){
pageNumber--;
if (pageNumber < 0)
return null;
search();
nextBool = false;
return null;
}
public void DeleteAcc(){
system.debug('ddddddddddddd');
if ( openjobsId == null ){
return;
}
Open_Job__c tobeDeleted = null;
for( Open_Job__c a : OpenJobsTop )
if ( a.Id == openjobsId ) {
tobeDeleted = a;
system.debug('tobeDeleted tobeDeleted '+tobeDeleted );
break;
}
if ( tobeDeleted != null ) {
tobeDeleted.Email_Bounced_Back__c= True;
system.debug('tobeDeleted'+tobeDeleted);
Update tobeDeleted;
}
}
}
Thanks.
Its too long ..
If any object is there..in your testclass create that object & assign value to the fields & use that in your existing business logic.
Please try below code :- NOTE: This code has not been tested and may contain typographical or logical errors
NOTE:- Always follow below best pratice for test classes.
1) Make calls to methods using both valid and invalid inputs.
2) Complete successfully without throwing any exceptions, unless those errors are expected and caught in a try…catch block.
3) Always handle all exceptions that are caught, instead of merely catching the exceptions.
4) Use System.assert methods to prove that code behaves properly.
5) Use the runAs method to test your application in different user contexts.
6) Exercise bulk trigger functionality—use at least 20 records in your tests.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
Thanks
Amit Chaudhary
amit.salesforce21@gmail.com