[SalesForce] Why does this SOQL statement return no results

The line inside the loop in the following code is reported as having no test coverage. This is corroborated by other code that uses the set not having test coverage either.

Previously I was not using SOQL directly, but was using the equivalent methods to get all records in the table (Sobject.getAll() and related) with the same results.

SSCCE:

private static Set<Id> getObjectIds() {
  Set<Id> result = new Set<Id>();
  for (MyTable t : [SELECT Id FROM MyTable]) {
    result.add(t.Id); // This line has no test coverage.
  }
  return result;
}

However, if I execute the following SOQL manually against the same org (Force.com IDE), it returns results.

[SELECT Id FROM MyTable]

What can cause this?

P.S. – I should probably use MyTable.getAll().keySet() but again, that returns an empty set which brings me right back to this question.

Also, we can rule out security since the test user should be able to see pretty much everything anyway and the data in this table is readable by any user.

Best Answer

Test methods do not have access to pre-existing data in your Org unless you use @IsTest(SeeAllData=true). I would not recommend this though as if you are dependant on certain data being available then your tests may not passed when moved to another Org.

To fix this you need to create some data within your tests, e.g.:

MyTable testMyTable = new MyTable();
insert testMyTable;

This also makes it easier for you to write your asserts as you know exactly what data you should be testing against rather than relying on the correct data being in the Org you are testing on, e.g.:

MyTable testMyTable = new MyTable();
testMyTable.MyField = 'Test';
insert testMyTable;

System.assertEquals('Test', testMyTable.MyField);

The documentation covers this here.

Starting with Apex code saved using Salesforce.com API version 24.0 and later, test methods don’t have access by default to pre-existing data in the organization, such as standard objects, custom objects, and custom settings data, and can only access data that they create. However, objects that are used to manage your organization or metadata objects can still be accessed in your tests such as:

  • User
  • Profile
  • Organization
  • AsyncApexJob
  • CronTrigger
  • RecordType
  • ApexClass
  • ApexTrigger
  • ApexComponent
  • ApexPage
Related Topic