+ Start a Discussion
chris_centrachris_centra 

WebServiceMock from batch process - uncommitted work pending exception

Hello.  I started using the the WebServiceMock interface shortly after it came out, but there was an issue where it would throw the "uncommitted work pending" exception.  this is found elsewhere in the forums.  i recently had the need to use WebServiceMock again - and i found that the issue was resolved (https://developer.salesforce.com/forums?id=906F0000000927CIAQ) - so i tried it.  but i'm still having the same issue - sometimes.   My callout happens either as the result of a trigger execution or a batch process.  With the trigger, it works perfectly - no "uncommited work pending" exception.  however when i call it from my batch process, i'm still seeing the same issue.

a few notes here.  i've confirmed that all of my code is v29.  in my batch process, i disable the trigger - so it's not like the callout is firing multiple times.  i've implemented my test method per the instructions noted in the linkd above (first setup data, then calling Test.startTest, then setMock, then do callout, then call stopTest.  i've checked the debug log thoroughly - there is no dml between my Test.startTest and the callout.  so this is a bit mysterious to me.  is anyone seeing a similar issue?

thanks
chris
Vinita_SFDCVinita_SFDC
Hello Chris,

Please refer following help document addressing this issue:

https://help.salesforce.com/apex/HTViewSolution?urlname=You-have-uncommitted-work-pending-Please-commit-or-rollback-before-calling-out-1327108312571&language=en_US
chris_centrachris_centra
right, i know how to test this in the real world, but this isn't something that i can implement in test coverage, is it?  maybe i'm missing something....
thanks
chris
Jason HartfieldJason Hartfield
I ran into this same issue today when trying to test a batch process that does callouts.  Did you ever open a case for this so it is on SFDC's radar?
chris_centrachris_centra
yep, i did open a case on this.  i did not find their response helpful.  they said:

You have to split the execution of the callout into another class and then avoid execution of that class if a test is running.
Then have another test class to get coverage for the class doing the callout.
We can use isRunningTest() method in batch class to avoid the execution of newly created class(for HTTP callout) while running a test.


maybe i'm just not understanding this response, but i don't think this gets to the root of the issue.  i don't want to completely avoid execution of the callout - instead i want it to use the WebServiceMock functionality as it is intended.  it's frustrating.  it seems like WebServiceMock comes so close to giving me exactly what i need, but it has a flaw which renders it useless in this scenario.  please let me know if their response means something to you...

thanks
chris
Jason HartfieldJason Hartfield
Yes, their response does not get to the root of the issue. It is just a workaround. So in the batch method, I have the piece that does the callout wrapped with a if(!test.isRunningTest()) statement, and then I run the test (I can't do any useful assertions at the end since the callout is what actually does everything). Then I have a separate test that tests the piece that was skipped in a different test altogether.
charles jonascharles jonas
I also ran into this issue.  Basicly preventing any useful test code to be written on a critical part of an application.  

Sometimes salesforce can be a real disapointment.  They should at least update their documenation to explain this...