+ Start a Discussion
Ben MorchBen Morch 

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

Hello,

I am trying to create a test class for a security check class.  I am using a SOQL query to get an account and pass that along to the class.  When I do a Run Test in the sandbox, it give me the following error:

System.QueryException: List has no rows for assignment to SObject
Class.SecurityCheckTest.securityCheckTest: line 32, column 1

I believe this is telling me that the query is not returning any data to put into the "acct" attribute.  But I have put a "where id != NULL" on the query.  Can anyone give me a suggestion as to why I would still be getting this error?

Line 32 -  

acct = [select id,name,ownerId,BillingAddress,ShippingAddress,Billing_Address__c,Shipping_Address__c,BillingStreet ,BillingCity,BillingState,BillingPostalCode,BillingCountry,
                                    ShippingStreet,ShippingCity,ShippingState,ShippingPostalCode,ShippingCountry,CreatedBy.name,lastModifiedBy.name ,CreatedDate,createdby.id,lastmodifiedby.id,
                                    lastmodifieddate,Ultimate_Parent_Owner_ID__c,lastActivityDate,account_availability__c,Account_Link__c,Account_Hierarchy_Link__c
                                    from account where id != NULL limit 1];
Test Class -
@isTest
private class SecurityCheckTest {

    static testMethod void securityCheckTest() {
      
      boolean showing;
      User runThis = [Select id, alias, email, username, userRoleId, ProfileId From User Where userRoleId != NULL limit 1];
      system.debug('runThis user ==> ' + runThis);
      
      Account acct = new Account();
      acct = [select id,name,ownerId,BillingAddress,ShippingAddress,Billing_Address__c,Shipping_Address__c,BillingStreet ,BillingCity,BillingState,BillingPostalCode,BillingCountry,
                                    ShippingStreet,ShippingCity,ShippingState,ShippingPostalCode,ShippingCountry,CreatedBy.name,lastModifiedBy.name ,CreatedDate,createdby.id,lastmodifiedby.id,
                                    lastmodifieddate,Ultimate_Parent_Owner_ID__c,lastActivityDate,account_availability__c,Account_Link__c,Account_Hierarchy_Link__c
                                    from account where id != NULL limit 1];
    insert acct;  // is this necessary?
        
        system.debug('acct id ==> ' + acct.id);
        system.debug('Name ==> '+ acct.Name);
        system.debug('UPO ID ==> ' + acct.Ultimate_Parent_Owner_ID__c);
        if(acct!=NULL){
      SecurityCheck sec = new SecurityCheck(acct);
      system.debug('acct ==> ' + acct);
      system.debug('currRecord ==> ' + sec.currRecord);
      system.RunAs(runThis){
        showing = sec.showPage();
      }
        } else { system.debug('==> Account is empty'); }
    
    
    system.debug('showing ==> ' + showing);
    }
}
Thanks for any help that you can give.
Best Answer chosen by Ben Morch
Jerome LusinchiJerome Lusinchi
Hi,

try 
@isTest(SeeAllData=true)
at the begining of your test class

Jerome

All Answers

Jerome LusinchiJerome Lusinchi
Hi,

try 
@isTest(SeeAllData=true)
at the begining of your test class

Jerome
This was selected as the best answer
Ben MorchBen Morch
Jerome,

Thank you for your reply.  That was the answer I needed.  I also removed the "insert acct;" as this was not necessary either.