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

test method issue

This is my code stub that catches nullPointerException and I am trying to pass coverage on the 2nd test method testAccountTerritoryUpdateNegative() below.  I also know that I am not supposed to hardcode ID or Name in the test method but I will fix this later since I'm just trying to implement the test to see if it passes. How come the soql in my test method brings back zero rows? When I run that soql in the schema explorer, it brings back the record.  hmmmmmmm


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

Class.AccountTests.testAccountTerritoryUpdateNegative: line 51, column 1


catch (System.NullPointerException e)
for(Account a: aList)
a.addError('There was an error with the record since a field was not populated correctly, please contact IT at 1888-888-8888 for asssitance, error number ' + e.getLineNumber());


@isTest private static void testAccountTerritoryUpdateNegative()
Account testAccount =[select id, billingstreet from account where name = 'test compant'];
testAccount.BillingStreet ='test negative street';
update testAccount;
catch(System.NullPointerException e)
System.assertEquals('NullPointerException' , e.getTypeName());


Best Answer chosen by Admin (Salesforce Developers) 

The reason is since API v24 onwards  you can't automatically access data in the org from the test method, but you can relax this restriction (using the SeeAllData=true annotation) or use an earlier API version.


See the salesforce docs: for full information.


The best answer however is to always create the data you need in your test method first, then you can be sure of getting exactly the results you need to test for and not rely on pre-existing data in any instance.