2

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.: 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);
}    

 }

Thanks

  • Can you please post the GenerateJobOfferController constructor as well? – highfive Nov 13 '14 at 5:23
3

Your failure is in the constructor of GenerateJobOfferController.

A query (on line 6) is not finding any records and you are assigning it directly to a single sObject. This causes salesforce to throw an exception.

Without seeing the code for that class its hard to give more specific details. Are you using a page parameter in a query?

acc = [SELECT Id, ... FROM Account WHERE Id = :ApexPages.currentPage().getParameters().get('id')];

If so, you need to set it up in your test:

static testMethod void myUnitTest() {
    // TO DO: implement unit test
    LoadData();

  //set up query param        
   Test.setCurrentPageReference(new PageReference('Page.myPage'));
    System.currentPageReference().getParameters().put('id', objAccount.Id);

    ApexPages.StandardController stdController = new ApexPages.StandardController(objApplication);
    GenerateJobOfferController objGenerateJobOfferController = new GenerateJobOfferController(stdController);

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.