You need to sign in to do that
Don't have an account?
Saran
Regarding Chatter
Hi All,
I tried to send a mock callout after inserting the collabrotion group in apex test calass. I am getting an error "You have uncommited work pending".
Code snippet:-
CollaborationGroup myGroup = new CollaborationGroup();
myGroup.name = 'Test';
myGroup.OwnerId =userId;
myGroup.CollaborationType= 'Public';
insert myGroup;
Test.startTest();
Test.setMock(HttpCalloutMock.class, mock);
// calling callout methods.
Test.stopTest()
Though both are differnt transaction it shows the above error message.
Thanks in Advance.
saraz
I tried to send a mock callout after inserting the collabrotion group in apex test calass. I am getting an error "You have uncommited work pending".
Code snippet:-
CollaborationGroup myGroup = new CollaborationGroup();
myGroup.name = 'Test';
myGroup.OwnerId =userId;
myGroup.CollaborationType= 'Public';
insert myGroup;
Test.startTest();
Test.setMock(HttpCalloutMock.class, mock);
// calling callout methods.
Test.stopTest()
Though both are differnt transaction it shows the above error message.
Thanks in Advance.
saraz
Here’s the low down on how to get around the “You have uncommitted changes pending please commit or rollback…” when trying to mix DML and HTTPCallouts in your test methods.
First, a little background and a health and safety warning. Sooner or later you’ll be faced with testing a method that both a: manipulates existing data, and b: calls out to a third party service for more information via HTTP. Sadly, this is one of those situations where testing the solution is harder than the actual solution. In a testing situation, you should be inserting your data that your method is going to rely on. But this making a DML call — insert — will prevent any further http callouts from executing within that Apex context. Yuck. That means inserting say, an account, and then making a call out with some of that data … well that just won’t work. No Callouts after a DML call.
So lets cheat a bit. Apex gives us two tools that are helpful here. The first is the @futureannotation. Using the @future annotation and methodology allows you to essentially switch apex contexts, at the cost of synchronous code execution. Because of the Apex context switch, governor limits and DML flags are reset. Our second tool is a two-fer of Test.startTest() and Test.stopTest(). (you are using Test.startTest() and Test.StopTest() right?) Among their many tricks is this gem: When you call Test.stopTest(); all @future methods areimmediately executed. When combined together these two tricks give us a way to both insert new data as part of our test, then make callouts (which we’re mocking of course) to test, for example, that our callout code is properly generating payload information etc. Here’s an example:
This test works, because we can both a: switch to an asynchronous Apex context that’s not blocked from making HTTP Callouts, and b: force that asynchronous Apex context to execute at a given time with test.stopTest().