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
Sirisha KSirisha K 

Test class for a controller that has logic to query the external object records.

We have enabled the Lightning connect and defined an External Data Source for Lightning Connect - Odata 2.0 Adapter and the External Object records are loaded from Informatica. The functionality basically displays all the external object records related to an Account (External object is related to Account - indirect lookup relationship), on the Account detail page as an inline VF page. When the external object records are queried, in the background a REST callout is made. So the Test class needs to use a Test.setMock() method to set a mock response. Here is the code snippet that I'm using:

 static testMethod void PositiveTestCase() {
// test user - currentUser
system.runAs(currentUser){

//Setup Test Data
//Mock response
String testResp = 'CUSTOMER_SALES_HISTORY__x:{DisplayUrl=TEST,CUSTOMER_SALES_HISTORY_IID__c=TEST,ExternalId=TEST,SAP_CUSTOMER_NUMBER__c = 0000001003, PRODUCT_LINE__c  = LINEA, PRODUCT_NAME__c =PRD1 PRODUCT_TYPE__c = TYPEA}, CUSTOMER_SALES_HISTORY__x:{DisplayUrl=TEST1,CUSTOMER_SALES_HISTORY_IID__c=TEST1,ExternalId=TEST1,SAP_CUSTOMER_NUMBER__c =1234, PRODUCT_LINE__c  = LINEA, PRODUCT_NAME__c =PRD2 PRODUCT_TYPE__c = TYPEB}';

//Test class that implements HttpCalloutMock and returns a reusable response
Test_ReusableMockResponse fakeResponse = new Test_ReusableMockResponse(200,'SUCCESS',testResp, null);

Test.setMock(HttpCalloutMock.class, fakeResponse);
Test.startTest();
//set the standard Controller to the test Account created
ApexPages.StandardController std = new ApexPages.StandardController(testAccount);
InlineOrderHistoryViewController controllerInstance = new InlineOrderHistoryViewController(std);
Test.stopTest();
}
}

 
For a straightforward - web service built on REST, this mock response approach should ideally work. Considering the mock response as the real time response received, the unit test case scenario should cover the code.
But in this case, the mock response is not considered as the real time response in the unit test method and all the subsequent lines of code aren't covered.
Also, the Test.setMock() does help in overcoming -' Test methods to not support the WebService Callout' error. So I'm totally convinced that I need to cover this the way we cover the code involving web service callouts.

Please let me know if you have a workaround for this

Thanks,
Sirisha Kodi
Best Answer chosen by pcon
pconpcon
Unfortunately it doesn't seem like you can test this the way you normally would.  I was talking with Pat Patterson [1][2] and the only way to do this is to carefully wrap your remote object queries with isRunningTest and return data there.  The ability should be "coming soon."

[1] https://twitter.com/metadaddy/status/659074056369500160
[2] https://twitter.com/metadaddy/status/659075008757567488

All Answers

pconpcon
Unfortunately it doesn't seem like you can test this the way you normally would.  I was talking with Pat Patterson [1][2] and the only way to do this is to carefully wrap your remote object queries with isRunningTest and return data there.  The ability should be "coming soon."

[1] https://twitter.com/metadaddy/status/659074056369500160
[2] https://twitter.com/metadaddy/status/659075008757567488
This was selected as the best answer
Abi V 4Abi V 4
Hello All,
I am really in the need of doing the same.
My test class having SOQl which is not covering the code coverage for external objects and i need to deploy this in PROD.Please suggest me how to achive this?
Abi V 4Abi V 4
Please post me some ideas if there are any articels/formus related to the same.
Joseph MurawskiJoseph Murawski
Any answers on including external objects (__x) in test classes?
pconpcon
There is still no way to test external objects inside of test classes.  You still have to return data based on Test.isRunningTest.