Comments (4)
@pellmr -- can you provide a very concise repro case here? Something doesn't sound right -- we have UnitOfWork create data in our TestSetup method and then do callouts in our test methods (using Test.setMock first, of course) and have never seen the error you're reporting.
from fflib-apex-common.
Hmm...I'm probably just doing something wrong then. Here is a contrived example for simplicity.
Class
public without sharing class UowIssueDemo {
public static void makePostCalloutPreWork() {
Contact testContact = [SELECT id FROM Contact LIMIT 1];
makePostCallout(testContact.Id);
}
@future(callout=true)
public static void makePostCallout(Id contactId) {
Contact c = [SELECT FirstName FROM Contact LIMIT 1];
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://fflibuow.free.beeceptor.com/contact');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
request.setBody('{"name": "' + c.firstname + '" }');
HttpResponse response = http.send(request);
if (response.getStatusCode() != 201) {
System.debug(
'The status code returned was not expected: ' +
response.getStatusCode() +
' ' +
response.getStatus()
);
} else {
System.debug(response.getBody());
}
}
}
Test Class
@isTest
public with sharing class UowIssueDemoTest {
@isTest
static void testPostCallout() {
//Arrange
fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(
new List<SObjectType>{ Contact.SObjectType }
);
uow.registerNew(new Contact(FirstName = 'DemoGuy', LastName = 'Firebird');
uow.commitWork();
Test.setMock(HttpCalloutMock.class, new UowIssueHttpCalloutMock());
//Act
Test.startTest();
UowIssueDemo.makePostCalloutPreWork();
Test.stopTest();
//Assert
System.assertEquals(Limits.getFutureCalls(), 1, '1 future should be called');
}
}
Error
UowIssueDemoTest.testPostCallout UowIssueDemo Fail 92% System.CalloutException: You have already created Savepoints. You cannot make callout after creating a Savepoint
from fflib-apex-common.
So, if you are using the UnitOfWork from the Apex Common framework are you also using the Selector pattern?
If you are using the Selector pattern, then are you using Apex Mocks in the unit tests? Doing so would solve this issue.
from fflib-apex-common.
This appears to be an undocumented limitation of @future
and Queueable methods with regard to Savepoints. I can indeed repro the problem as reported. And if I replace SOUOW with a straight DML insert of a new Contact, the test completes (it fails for another reason -- the assertion to test the number of Future calls never increments -- but that's not relevant here)
We are not going to change the SOUOW behavior in unit test context. The risk of masking failures in production code execution context by not using the same code path in unit test context far outweighs the impact of this corner case.
As an aside: future methods have long fallen out of favor -- Salesforce introduced Queueables a decade ago and has steadily increasing the flexibility and feature set of Queueables -- I am not aware of any use case of @future
that can't be done equivalently with Queueable. But again, Queueables won't save the day here; they are subject to the same limitation.
The only workaround I can offer is what I posted in my first comment. If you do the SOUOW commitWork in a @TestSetup
method, that is compatible with using an async/callout in a test method.
from fflib-apex-common.
Related Issues (20)
- Unit test failure in multi-currency org HOT 4
- Deploy button doesn't deploy because there are test failures HOT 1
- Expected a QueryException due to read only user not having access to Opportunity HOT 7
- Switch to Inherited Sharing on SObjectDescribe and SObjectSelector HOT 2
- With latest fflib, do TriggerHandlers become service class consumers? If so what happens to UoW? HOT 6
- Selected tests in fflib_SObjectSelectorTest fail in an org with encryption enabled on Account.Name HOT 2
- Selector Mocks to Include SOQL Query Retrieve Check HOT 1
- fflib_SObjectUnitOfWork doCommitWork executes all dml for all registered types even if there are no changes
- Update README as session recordings are not available HOT 2
- fflib_SObjectDescribe.cls fails to resolve cross-object field paths for Person Accounts
- Aggregate SOQL support. HOT 2
- Inconsistent Code Coverage and Test Failures in fflib-apex-common HOT 1
- fflib_SObjectSelector and fflib_SObjectUnitOfWork have insufficient test code coverage. HOT 2
- Add queryWithBinds to Selector layer HOT 2
- Unit test failing fflib_SecurityUtilsTest & sysadmin_objectAndField_access HOT 8
- Coverage of UnitOfWork class HOT 1
- Versioning HOT 1
- fflib_SObjectSelectorTest failure with Lookup relationship and Person Type Accounts HOT 3
- fflib_SObjectSelectorTest Error: 'Assertion Failed: Expected: 12345.67, Actual: 9117.25' HOT 13
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fflib-apex-common.