+ Start a Discussion

test code error - list has no rows for assignment to SObject

I am trying to deploy a change set from a sandbox and an existing trigger is preventing me from doing so.  The test does not have the required coverage.  The error I get is System.QueryException: List has no rows for assignment to SObject.  The test code is as follows:

private class LeadTriggerTest {

    static testMethod void LeadTest() {
       String testleadid='';
       Lead testlead = new lead();
       testlead.country='United Kingdom';
       //get one user of proper type, use the where clause to filter
       user u=[select id,name from user where user.name='jonathan parker' LIMIT 1 ];
            insert testlead;
            update testlead;
            testleadid=testlead.id;  //if created correctly
          }catch(DMLException e){
             system.assert(false, 'Error occurred inserting test record:'+e.getDMLMessage(0));
         Lead checkcase = [select id,name from lead where id=:testleadid];


Best Answer chosen by Admin (Salesforce Developers) 
Andy BoettcherAndy Boettcher

You have to create that user in your test class first - by default test classes do not have any access to Production data, such as Users.