0

On February 2015 we have noticed some tests started failing suddenly on Production. We have opened a Salesforce support case and we were told that we are affected by known issue.

Recently that issue was marked as resolved but when I tried to run my simple test case scenario I got these errors again.

Simplified business logic test case scenario is following.

  1. Create custom object Test_Object.

  2. Create apex class and create a chatter group and use its id on line 10

    public class TestServices {
        @future(callout=true)
        public static void doSomeDummyCallOut() {
            Http h = new Http();
            HttpRequest req = new HttpRequest();
            req.setEndpoint('http://goo.gl'); 
            req.setMethod('GET');
            HttpResponse res = h.send(req);
    
            insert new FeedItem(Body='x' + res.getStatus(),ParentId='chatterGroupId');
        }
    }
    
  3. Create a trigger on test object with code

    trigger TestTrigger on Test_Object__c (after insert, after update) {
        TestServices.doSomeDummyCallOut();
    }
    
  4. Create test class TestTestTrigger with code

    @isTest
    private class TestTestTrigger {
        @isTest
        static void test1() {
            CollaborationGroup chatterGroup = new CollaborationGroup(Name = 'x', CollaborationType='Public');
            insert chatterGroup; 
            Test.startTest();
                Test.setMock(HttpCalloutMock.class, new MockHttpResponse());
                insert new Test_Object__c(Name = 'x');
            Test.stopTest();
        }
        @isTest
        static void test2() {
            Test.startTest();
                Test.setMock(HttpCalloutMock.class, new MockHttpResponse());
                Test_Object__c t = new Test_Object__c(Name = 'x');
                insert t;
                update t;
            Test.stopTest();
        }
        @isTest
        static void workAround() {
            Test.startTest();
            Test.setMock(HttpCalloutMock.class, new MockHttpResponse());
                insert new Test_Object__c(Name = 'x');
            Test.stopTest();
        } 
    }
    

Run the unit tests. Before Spring 15 Release all of these tests were executed successfully. After [including] Spring 15 release two of them are failing with "System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out" error. enter image description here enter image description here Does anyone know about any good workaround for this issue?

Since commenting out business logic in test is not really a good workaround.

0

Today I have made a great discovery.

If we change API Version of Class which contains Callout to 28, the tests are not failing! It is amazing! enter image description here I believe I have tried the same two years ago and one year ago and this workaround didn't work then.

  • Good find! One of the weird ways of SF i guess!! – Shalini SF Dec 1 '16 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.