I know test methods should not see any data except the data created in test context, but when I run the test below, the query returns an org record.
It should not
I have a REST class like
@RestResource(urlMapping='/path/to/*')
global with sharing class MyRest {
@HttpPost
global static String executePost(){
String command = RestContext.request.requestUri.split('/')[3];
if (command == 'conf')
{
return noteConf(RestContext.request.requestBody);
}
// irrelevant code here...
}
private static String noteConf(Blob requestBody){
String title = 'Check connected app';
ContentNote[] ln = [Select Id, Title, Content from ContentNote where Title = :title];
ContentNote n;
if (ln.size()>0) {
n = ln[0];
}
else{
n = new ContentNote();
n.Title = title;
}
n.Content = requestBody;
upsert n;
return n.Id;
}
My test method is :
@isTest(seeAllData=false)
static void test_note(){
Test.startTest();
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = '/path/to/conf';
req.httpMethod = 'POST';
req.requestBody = Blob.valueOf('the note content !');
RestContext.request = req;
RestContext.response= res;
String s = MyRest.executePost(); //First call, a note should be created
Id i = (Id) s; //checks it's an Id (if not an Exception will be thrown)
String t = MyRest.executePost(); //call it again to check a 2nd note is not created
System.assertEquals(i, (Id) t);
Test.stopTest();
}
I expect the first SOQL query in noteConf
method to return 0 record, and the second one to return 1.
But it does
But there already is in my org a ContentNote with title 'Check connected app', the 1st query returns this record. I can't figure out why ?
The seeAllData=false was not there at the beggining, it's the default. Adding it didn't change anything.
Here is the log of the 1st call :
19:01:31.3 (16682032)|SOQL_EXECUTE_BEGIN|[50]|Aggregations:0|SELECT Id, Title, Content FROM ContentNote WHERE Title = :tmpVar1
19:01:31.3 (48338272)|SOQL_EXECUTE_END|[50]|Rows:1
19:01:31.3 (48381399)|HEAP_ALLOCATE|[50]|Bytes:8
19:01:31.3 (48412157)|HEAP_ALLOCATE|[50]|Bytes:329
19:01:31.3 (48547547)|HEAP_ALLOCATE|[50]|Bytes:8
19:01:31.3 (48579122)|VARIABLE_SCOPE_BEGIN|[50]|ln|List<ContentNote>|true|false
19:01:31.3 (48653446)|VARIABLE_ASSIGNMENT|[50]|ln|[{"Id":"0693E000000hfsIQAQ","Title":"Check connected app","LatestContentId":"05T3E000002GTDjUAO","LatestPublishedVersionId":"0683E000000hadgQAA","Content":BLOB(178 bytes)}]|0x26e3554
Wondering if the ContentNote object could have a different behaviour and been always seen, I added this code right after the SOQL query :
Contact[] lc = [Select Id from Contact];
System.debug(lc.size());
which returned 0 while, obviously, there are contacts in my org. That might be a clue...
I checked Salesforce documentation but but did not find anything about that, any help or explanation would be appreciated.