+ Start a Discussion
sreekanth reddysreekanth reddy 



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';
            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;
                if(opens.Dummy_Job_Link__c == 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){
        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';
           insert jobSearchA;
        PageReference customPage = new PageReference('/'+opportunityID.Id);
        return customPage;
        return null;
     //Custom Pagination for next
    public PageReference next(){
        prevBool = false;
        return null;
  //Custom Pagination for previous
    public PageReference previous(){
        if (pageNumber < 0)    
            return null; 
        nextBool = false; 
        return null;
     public void DeleteAcc(){
        if ( openjobsId  == null ){
        Open_Job__c  tobeDeleted = null;
        for( Open_Job__c a : OpenJobsTop )
            if ( a.Id == openjobsId ) {
                tobeDeleted = a;
                  system.debug('tobeDeleted tobeDeleted '+tobeDeleted );
        if ( tobeDeleted != null ) {
            tobeDeleted.Email_Bounced_Back__c= True;
            Update tobeDeleted;

Kunal GhoshKunal Ghosh

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.
Amit Chaudhary 8Amit Chaudhary 8

Please try below code :-
public class JSS_JobSearchTest
    public static testMethod void unitTest()
       Account acc= new Account();
       acc.Name ='Test Account';
       insert acc;
       Contact cont=new Contact();  
       cont.FirstName ='test FirstName';
       cont.LastName ='test Last';
       cont.accountId = acc.id;
       insert cont;
	   Opportunity opp = new Opportunity();
	   opp.AccountId =acc.id;
	   opp.JSS_Countries__c = 'Test JSS_Countrie';
	   opp.JSS_Industry__c =  'Test Industry';
	   opp.Sub_Industry__c =  'Sub Ind';
	   insert opp.

		// Please add All required field data and modify data type
	   Open_Job__c openJob = new Open_Job__c();
		   openJob.Country__c = 'Test JSS_Countrie';
		   openJob.Industry__c = 'Test Industry';
		   openJob.Sub_Industry_oj__c = 'Sub Ind';
		   openJob.Expiry_Date__c = System.today() + 10;
	   insert openJob;

	   // Please add All required field data and modify data type
	   Job_Search__c jobSearch = new Job_Search__c();
			jobSearch.Country__c ='Test JSS_Countrie';
			jobSearch.Industry__c ='Test Industry';
			jobSearch.Opportunity__c = opp.id;
	   insert jobSearch;

			  ApexPages.currentPage().getParameters().put('id', opp.id);
				JSS_JobSearch controller = new JSS_JobSearch(); 
				//controller.commits(); call all function one by one
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

Amit Chaudhary