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
teknicsandteknicsand 

Apex Test Class

User opp_owner = [select id, username, Profile.Name, email from User where id = :opp.OwnerId];

 

How do I test for the above query in a test class? When I call the function to which the above line belongs, in the test class, I get a "List has no rows for assignment to SObject" error.

 

Any thoughts or suggestions?

 

Thanks in advance..

 

Sand

jkucerajkucera

Not my area of expertise, but I'd imagine you could do something like:

 

User u=new user();

insert u;

//assign required user fields u.name="name", etc; Opportunity o=new Opportunity(); //assign required opportunity fields o.userId=u.id; insert o; //call your method

 

 

 

teknicsandteknicsand
Ah, I tried that already and I get a INVALID CROSS REFERENCE_KEY; owner cannot be blank[ownerid] error for opp.ownerid=u.id
Message Edited by teknicsand on 07-27-2009 03:19 PM
teknicsandteknicsand

User opp_owner = [select id, username, Profile.Name, email from User where id = :testopp.OwnerId];

 

Isn't an opportunity created in a test class assigned to any owner? The above line in the test class returns with no rows for list error. Testopp is created and inserted successfully. But when I try and query for an user who owns it, I get the error.

 

Am I missing something here?

SuperfellSuperfell
did you query your opp back after you created it? any fields defaulted by the insert process won't be populated on your source object.
teknicsandteknicsand

I queried the opp I inserted. Too bad I can't look at the debug values, since the **bleep** debug log size reached its limit!!! :smileymad:

 

So are you saying that fields that are defaulted during the insert process wont be populated with anything?