+ Start a Discussion
mavsmavs 

help on Test method

 

Below is my Apex class and test method. when i run my test i am receiving the error

 

System.QueryException: List has no rows for assignment to SObject (and this is for the line where i am quering sales_office custom object)

 

Can some one please guide me how to write test methos for the code below?

 

Thanks very much in advance for the help

public class Accountdelete { Id AcctId = ApexPages.currentPage().getparameters().get('id'); public Account getAccountName() { return [Select Name from Account where id=:AcctId]; } public PageReference Reassign() { Account acct=[Select Named_Account__c,RegionName__c,ownerid from Account where id=:AcctId]; Sales_Office__c salesoffice=[Select sales_Administrator__c from sales_Office__c where name=:acct.RegionName__c]; String res=''; Integer count=(acct.product_type__c).indexOf('QAC'); if(count>-1) { res=(acct.product_type__c).replace('QAC',''); } acct.product_type__c=res; acct.ownerid=salesoffice.Sales_Administrator__c; update acct; return null; } public static testMethod void results() { Id id; Account testAccount =new Account(Name='Sample2',RecordTypeid='0123000000002GvAAI',NumberOfEmployees=10,BillingStreet='71 Maple st, BillingCity='Doral', BillingState='FL', BillingPostalCode='33172', BillingCountry='US',phone='1111111111',RegionName__c='Miami'); insert testAccount ; Sales_Office__c Salesoffice=new Sales_Office__c(SALES_ADMINISTRATOR__C='00530000000f5H6AAI',NAME=testAccount.RegionName__c); insert Salesoffice; ApexPages.currentPage().getParameters().put('id', testAccount.id ); id = ApexPages.CurrentPage().getParameters().get('id'); Accountdelete test=new Accountdelete(); test.getAccountDetails(); test.Reassign(); } }

 

Message Edited by mavs on 07-30-2009 01:51 PM
snugglessnuggles

hi mavs,


 

I would first make sure to account for the situation that the error is telling you about. Take that line where you are initializing your acct variable for example, if your query doesn't find an account that matches that ID, you will cause this error and it would be displayed to an end user

Message Edited by snuggles on 07-31-2009 03:34 PM
jwetzlerjwetzler

I am not sure it's going to work setting the parameter like that.  You should look into using setCurrentPage

 

Something like this:

 

PageReference testPage = Page.yourPageNameHere; testPage.getParameters().put('id', testAccount.id); Test.setCurrentPage(testPage);

 

replace 'yourPageNameHere' with the name of the page that uses this controller.

 

I think that should work.

 

KrishnadasKrishnadas

Hi,

You could try removing the line 

id = ApexPages.CurrentPage().getParameters().get('id'); 

from your test class. the line above it will assign value to the variable AcctId in your class.

 

 

Thanks

Krishnadas

Steadfast Global

www.steadfastglobal.com 

jwetzlerjwetzler
that line is a no-op, it doesn't do anything but assign it to a value that's never used, so that will have no effect on the way the test runs.