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
dmanidmani 

Test class Error for visualforce Page

Hi,

 

I am getting below error when i run the test class.

 

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

 

Error is in below line in controller:

 

User unamename = [Select id,Name,Email from User where id=:lecord.ownerid Limit 1];

 

 

From Visualforce Page i am passing the UserId to controller. IN test method iam not able to pass the UserId in the Above Query , hence Query fetches 0 Row and error occurs. How to Pass the Userid in test method. I tried using

 

apexpages.currentpage().getparameters().put('lecord.Id',Userid.id);

 

but still it fails. Guide me how to pass Pls.

Devender MDevender M
In the test Class you should also inset the user.
dmanidmani

Hi devender,

 

I tried inserting the user and passed the User id , but still the query fails.

 

IN test method i have followed below code:

 

Profile p = [SELECT Id,Name FROM Profile Limit 1];
User u = new User(Alias = 'test', Email='test@gmail.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='test@testorg.com');
insert u;

User Userid = [Select id,Name,Email from User where Alias = 'test' and Email = 'test@gmail.com'];

 

apexpages.currentpage().getparameters().put('lecord.Id',Userid.id);

User unamename = [Select id,Name,Email from User where id=:Userid.id Limit 1];

 

 

and in VF page:

 

<apex:inputField value="{!lecord.ownerid}" required="true" / >

 

i am passing User Id value to this input field. But i am not able to Cover this input in test class in the Query.

 

What am i missing? Help me pls

amarcuteamarcute

Hi,

 

you can use @isTest(SeeAllData = true) annotation at the calss level or at the method level to open up the data access.

You can check the complete documentation @ 

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_classes_annotation_isTest.htm|StartTopic=Content%2Fapex_classes_annotation_isTest.htm|SkinName=webhelp

amarcuteamarcute

OR You can also use the inserted user record Id instead of querying it again.

 

i.e. instead of the following 3 lines in your code

 

User Userid = [Select id,Name,Email from User where Alias = 'test' and Email = 'test@gmail.com'];

apexpages.currentpage().getparameters().put('lecord.Id',Userid.id);

User unamename = [Select id,Name,Email from User where id=:Userid.id Limit 1];

 

use 

 

apexpages.currentpage().getparameters().put('lecord.Id',u.id);

String userName = u.Name;

 

etc. Just reference the record you have inserted.