Goal - Avoid using SeeAllData=true for testmethods against APEX code that inserts
PartnerNetworkRecordConnection objects (to test outbound sharing)
What I believe to be true
PartnerNetworkConnectionobject can not be mocked - DML and test.loadData() both fail
PartnerNetworkConnectionrows are available for query even in a testmethod marked simply as
PartnerNetworkRecordConnectionrows can be inserted in an
@IsTesttestmethod. However, the inserted rows have to be assigned to a
OK, not great but as we're only relying on a real active Connection but otherwise we've isolated test data.
Now, here is where I am stumped
- If you insert a
PartnerNetworkRecordConnection(PNRC) through code (such as an Account), it will be assigned by SFDC a status of 'Invite'.
- If you then try and insert a
PartnerNetworkRecordConnectionfor, say, an Opportunity, where the parentRecordId is the Account you successfully inserted in #1 above, the insert will fail with
INVALID_PARTNER_NETWORK_STATUS. This is because the PNRC for the Account hasn't been moved to status = 'Sent' by SFDC.
So, how to mock the PNRC status to 'Sent' for the parent (e.g. Account) so the PNRC insert of the Opportunity succeeds?
Here is what I tried, all of which failed:
- Test.loadData(...) // internal Salesforce error
- DML update on PNRC // SObject is not updateable
- Placing Test.stopTest() after the PNRC insert of the Account, signalling SFDC to do "async-y stuff" like it does for future and batches. // INVALID_PARTNER_NETWORK_STATUS on the PNRC insert for the Opportunity
- Json.deserialize(..) into a PNRC object // doesn't help as the PNRC DML insert validates against a real database record, not an in-memory sObject
- S2S Best Practices - relies on already shared objects to use in testmethods; however, I believe this was written before Test Data Isolation was a feature
- SFSE post - with similar conclusions but 12 months old
- Numerous SFDC Developer forum questions from 2012 and earlier; all with same issue
Option 1 - Give up my aversion to SeeAllData and hence, manage in every sandbox + PROD a known parent Account successfully shared on a known PartnerNetworkConnection via Custom Settings
Option2 - Replace in my PROD code the line that does the Database.insert(..) on the PNRC with a method call to an interface which in PROD context does the DML but in testmethod context creates a local object that is verified by testmethod asserts (sort of a shadow database). But I'm not really testing the DML to verify it can actually work in the test.
Option 3 - Get my code coverage as high as possible with as many asserts as possible; make my Apex code be loggable and fault tolerant (not addError a transaction just because of an S2S error).
I'm currently doing Option 3 but I'd love to hear if one of the SFSE wizards have solved this issue.