You need to sign in to do that
Don't have an account?
Piotr B. Kożuchowski
Kind Regards
How can I insert test user for testing batch callout?
Hi All
When I create test user in unit tests and execute a batch that does callout, it fails with "Uncommited work pending" exception.
It happens only when I try to insert User, if I comment out "insert user" and leave any other SObject insertions, it will work just fine.
Do you know how can I get around this?
@TestSetup static void testSetup() { Profile p = [SELECT Id FROM Profile WHERE Name = 'System Administrator']; User usr = new User( FirstName = 'Test', LastName = 'User', Email = 'example@dev.sandbox.com', Username = 'example@dev.sandbox.com', ProfileId = p.Id, Alias = 'example', CommunityNickname = 'example', TimeZoneSidKey = 'GMT', LocaleSidKey = 'nl_NL', EmailEncodingKey = 'UTF-8', LanguageLocaleKey = 'en_US' ); insert usr; insert new Account(Name = 'Test'); } @IsTest static void testBehavior() { Test.startTest(); Test.setMock(HttpCalloutMock.class, new TestMock()); Database.executeBatch(new POC_BatchCallout()); Test.stopTest(); }The batch in this example does nothing else, but callout.
Kind Regards
https://salesforce.stackexchange.com/questions/11606/testing-batch-job-with-http-callouts
You need to do it like this
I'm not looking for a workaround, I want to know why inserting user makes callout fail whereas everything works fine with DML on any other object. This looks like an apex bug to me.