1

I have the following test method and keep getting: List has no rows for assignment to SObject.

    public static testMethod void TestChurchName() {

    PageReference pageRef = Page.ChurchHistoryDev;
    Test.setCurrentPage(pageRef);

    ChurchHistoryDevController con = new ChurchHistoryDevController();
    ApexPages.currentPage().getParameters().put('id', '1290');
    con.setChurchName();
    String ch = con.ChurchName;

    System.assertEquals('Metropolitan Baptist Church-Largo, MD', ch);
}

My here is my controller and the setChurchName() function.

    public ChurchHistoryDevController() {
    ChurchId = ApexPages.currentPage().getParameters().get('id');
}

//*****************************************************
// Church name get and set
//*****************************************************
public void setChurchName() {
    Account acct = [select Name from Account where Customer_ID__c = :ChurchId];
    ChurchName = acct.Name;
}

My visualforce page works fine. I just can't get the test to work and since SF is griping at me I guess I have to create tests :) Any idea what I'm doing wrong in my test? I know that it's blowing up because of the SELECT statement in setChurchName() but why isn't that value being passed to my class with the getParameters().put() method?

Thanks, Randy

1
  • 2
    Because you need to insert an Account prior to running your tests. Commented Jan 30, 2014 at 21:29

1 Answer 1

10

Because you need to insert an Account prior to running your tests.

Simply do the following prior to the test:

public static testMethod void TestChurchName()
{
    Account TestAccount = new Account(Name = 'Metropolitan Baptist Church-Largo, MD', 
        Customer_ID__c = '1290' /*Populate the account with other fields*/);
    INSERT TestAccount;
    PageReference pageRef = Page.ChurchHistoryDev;
    Test.setCurrentPage(pageRef);
    ApexPages.currentPage().getParameters().put('id', TestAccount.Customer_ID__c);

    ChurchHistoryDevController con = new ChurchHistoryDevController();

You can always put @isTest(SeeAllData=true) at the top of your test. But when you are running the test, you must always have the record there or the test will fail. That means, if someone accidentally deletes the record, your tests will fail. That includes when you are deploying because when you deploy to an org, those records won't exist. That's why I recommend the approach above (unless you cannot create the object in Apex - like a PriceBook).

5
  • 3
    To add additional emphasis, don't use seeAllData unless you have a compelling reason to. Dependending on actual data in the system for testing typically isn't a good idea. Unfortunately working with Products is one of those exceptions. Commented Jan 30, 2014 at 21:46
  • So when I do a test it don't actually see the real data that is there? I know I read the insert and update don't really perform the action but figured SELECT would grab real data? So I insert a fake record then query it basically?
    – Randy R
    Commented Jan 30, 2014 at 23:21
  • You don't see real data inside your org unless you specify with the annotation @isTest(SeeAllData=true). The INSERT and UPDATE are actually performed, but any data that is created and/or updated within a test are rolled back. This behaviour makes sense. Think of it this way, say one of your tests goes over a DELETE. If the tests could touch your data, you can possibly delete an important record by accident. Commented Jan 30, 2014 at 23:41
  • I just started working with SF recently (3 months ago) when our SF Admin left the company. My boss though since I'm an app developer I would be able to do stuff he couldn't do (he was not a programmer used wizards for everything) so instead of hiring someone else I got the honor :) I had never touched SF before and my boss hyped me up so much to the sales people I have like 4 projects on my burner while I'm still learning it :) I'm developing this Visual Force page on production because I'm not writing to anything and we don't have a full sandbox.
    – Randy R
    Commented Jan 31, 2014 at 1:38
  • I ran out of characters on last post. My sandbox is totally empty of records when I refresh it. I did try however to transfer my "custom object" from sandbox to production but could not get it to work. That will be another thread though if I can't figure it out. That's why I did this on production.
    – Randy R
    Commented Jan 31, 2014 at 1:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .