Uploading package with Apex Test class invoking HTTP callout fails if package has an aura bundle in it.
Apex , Packaging , Sandbox , Winter 18 , Lightning
Last updated 2018-06-26 ·Reference W-4429191 ·Reported By 14 users
No Fix
Summary
Creating a package with custom lightning component and an apex class invoking a HTTP callout, gives "System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out" during package upload.
Repro
1. Sign up a DE org
2. Create apex class
public class SimpleHttpCallout {
public String getCalloutResponseContents(String url) {
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');
HttpResponse res = h.send(req);
return res.getBody();
}
}
3. Create apex test class
@IsTest
public class SimpleHttpCalloutTest {
static testmethod void testCallout(){
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
SimpleHttpCallout simple = new SimpleHttpCallout();
simple.getCalloutResponseContents('http://www.salesforce.com');
}
public class MockHttpResponseGenerator implements HttpCalloutMock {
public HTTPResponse respond(HTTPRequest req) {
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody('{"foo":"bar"}');
res.setStatusCode(200);
return res;
}
}
}
4. Create lightning component
5. Apply namespace to the org, create managed package, add 2 apex classes and lightning component bundle and upload it
Package upload fails with following apex test failure
System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
Workaround
1. Avoid performing a callout in the apex tests if the package has aura bundle.
OR
2. Wrapping the tests in test.startTest() and test.stopTest().
Is it Fixed?
Any unreleased services, features, statuses, or dates referenced in this or other public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make their purchase decisions based upon features that are currently available.