I have the following query inside an @HttpGet REST API method:

SELECT Document_Id__c FROM Content_Link__c
GROUP BY Document_Id__c HAVING Count(Account__c) > 0

When run regularly I see the following in Debug Log:

13:06:43.0 (5132373)|SOQL_EXECUTE_BEGIN|[45]|Aggregations:0|SELECT Document_Id__c FROM Content_Link__c

13:06:43.0 (31522308)|SOQL_EXECUTE_END|[45]|Rows:800

When run from a test method (same org) I see the following in Debug Log:

13:21:49.0 (10552643)|SOQL_EXECUTE_BEGIN|[45]|Aggregations:0|SELECT Document_Id__c FROM Content_Link__c

13:21:49.0 (12525977)|SOQL_EXECUTE_END|[45]|Rows:0

This absence of any result rows is causing some lines to not execute during the test, reducing my code coverage. Any ideas what's causing this?


2 Answers 2


You're running into the Isolation of Test Data from Organization Data in Unit Tests. In other words, by default, unit tests can't see live data. This makes your unit tests more reliable, because they're not affected by bad data or large data volumes in production, but also means you need to insert data in the unit test before you do your testing. Don't worry, though, because any records you create won't be seen in production, and are removed at the end of the unit test.


Remember that when running tests they don't by default see your data. You either have to create the data for the test (the best option) or run with @isTest(SeeAllData=true).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .