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
salesforce_hoonigansalesforce_hoonigan 

Test class Failed: List has no rows for assignment to SObject

Hi Experts,

My test class is failing and I can't figure out why. Any assistance is greatly appreciated.

This is the error message:

System.QueryException: List has no rows for assignment to SObject

Stack Trace Class.GenerateJobOfferController.<init>: line 6, column 1
                   Class.GenerateJobOfferController_Tests.myUnitTest: line 15, column 1

This is the test class as a whole:
 
@isTest
private class GenerateJobOfferController_Tests {

    static Account objAccount;   
    static Contact objContact;
    static Job_Order__c objJobOrder;
    static Candidate__c objCandidate;
    static Candidate_Applications__c objApplication;
    
   
    static testMethod void myUnitTest() {
        // TO DO: implement unit test
         LoadData();
        ApexPages.StandardController stdController = new ApexPages.StandardController(objApplication);
        GenerateJobOfferController objGenerateJobOfferController = new GenerateJobOfferController(stdController);
       
   }
   
   static void LoadData(){
        createAccount();
        createContact();
        createJobOrder();
        createCandidate();
        createApplication();
        
   }
   
   static void createAccount(){
        objAccount = new Account();
        objAccount.Name = 'Test Account';
        
        insert objAccount;
        
        //assert..........
        Account TempAccount = new Account();
        TempAccount = [select id from Account where id =:objAccount.id];
        system.assertEquals(TempAccount.id == objAccount.id, true);
    }
    static void createContact(){
        objContact = new Contact();
        objContact.LastName = 'Test Contact';
        objContact.email = 'benX585@gmail.com';
        objContact.AccountId = objAccount.id;
        
        insert objContact;
        
        //assert..........
        Contact TempContact = new Contact();
        TempContact = [select id from Contact where id =:objContact.id];
        system.assertEquals(TempContact.id == objContact.id, true);
    }
    
    static void createJobOrder(){
        objJobOrder = new Job_Order__c();
        objJobOrder.Job_Title__c = 'Test Title';
        objJobOrder.Start_Date__c = date.today();
        objJobOrder.End_Date__c = date.today();
        objJobOrder.Status__c = 'Hiring';
        objJobOrder.Contact_Client__c = objContact.id;
        objJobOrder.Salary_Range__c = '123-123';
        objJobOrder.Experience_level_desired__c = 'Student';
        
        insert objJobOrder;
        
        //assert..........
        Job_Order__c TempJobOrder = new Job_Order__c();
        TempJobOrder = [select id from Job_Order__c where id =:objJobOrder.id];
        system.assertEquals(TempJobOrder.id == objJobOrder.id, true);
    }
    static void createCandidate(){
        objCandidate = new Candidate__c();
        objCandidate.Last_Name__c = 'Test Candidate';
                
        insert objCandidate;
        
        //assert..........
        Candidate__c TempCandidate = new Candidate__c();
        TempCandidate = [select id from Candidate__c where id =:objCandidate.id];
        system.assertEquals(TempCandidate.id == objCandidate.id, true);
    }
        
   static void createApplication(){
        objApplication = new Candidate_Applications__c();
        objApplication.Candidate_Status__c = 'New';
        objApplication.Job_Order__c = objJobOrder.id;
        objApplication.Candidate__c = objCandidate.id;
        
        insert objApplication;
        
        //assert..........
        Candidate_Applications__c TempApplication = new Candidate_Applications__c();
        TempApplication = [select id from Candidate_Applications__c where id =:objApplication.id];
        system.assertEquals(TempApplication.id == objApplication.id, true);
    }
    
  }

Thank you in advance.
Best Answer chosen by salesforce_hoonigan
Sfdc CloudSfdc Cloud
Hi,

First of all just wanted to check are you passing any id in Url. Then definatly problem would be related to parameter id.
You need to specify this attribute in your test class before calling your constructor.In this test you intialized constructor before setting VF page thats the reason parameter id would be null and SOQL returning no record.


pageReference refpage= Page.YourPageName;
ID pageid = refpage.getParameters().put('ParameterIdName',a2.ID); //parameter id which you are passing in your page
Test.setCurrentPage(pageid);
ApexPages.StandardController sc = new ApexPages.StandardController(objApplication);
GenerateJobOfferController controller = new GenerateJobOfferController(sc);

If this answer will help you out.Please mark it as best answer to help others
Thanks :)

All Answers

Navee RahulNavee Rahul
Hi honi,

are you receving this error in the production enviorment ?? or dev or sandbox??.

Please ensure that you have some records in Job_Order__c  object.looks like you have datas in Job_Order__c  in object,thats the reson may be its causing the bug.


Thanks
D Naveen Rahul.
Sfdc CloudSfdc Cloud
Hi,

First of all just wanted to check are you passing any id in Url. Then definatly problem would be related to parameter id.
You need to specify this attribute in your test class before calling your constructor.In this test you intialized constructor before setting VF page thats the reason parameter id would be null and SOQL returning no record.


pageReference refpage= Page.YourPageName;
ID pageid = refpage.getParameters().put('ParameterIdName',a2.ID); //parameter id which you are passing in your page
Test.setCurrentPage(pageid);
ApexPages.StandardController sc = new ApexPages.StandardController(objApplication);
GenerateJobOfferController controller = new GenerateJobOfferController(sc);

If this answer will help you out.Please mark it as best answer to help others
Thanks :)
This was selected as the best answer
salesforce_hoonigansalesforce_hoonigan
Thanks @Sfdc Cloud. I tested it and it worked for me. Thanks.