You need to sign in to do that
Don't have an account?
Shamina
System.CalloutException: You have uncommitted work pending error when creating testing user
There are a lot of questions related to "System.CalloutException: You have uncommitted work pending" errors, but could not find solution for following use case:
There's a static method in the test class which inserts a test user, a custom settings record, account and opportunity records. Then the test methods run as the testing user and covers code for a queueable apex that executes an HTTP callout.
When running the above test class, both test method fail with uncommitted work callout exception below:
System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
However, if only the lines of code for test user creation and system.runas is commented then the test class executes successfully as the running user.
It is recommended to run test methods as a testing user, but then how do we insert the test user in the test class and prevent the uncommitted work error ?
This known issue is similar but is marked as fixed since a long time, I am wondering if the issue still persists.
Any help is appreciated, thanks
There's a static method in the test class which inserts a test user, a custom settings record, account and opportunity records. Then the test methods run as the testing user and covers code for a queueable apex that executes an HTTP callout.
@isTest private class ProjectCalloutServiceTest { static List<Opportunity> oppLst; static Account acc; static User adminUser; static{ Id admProfId = TestFactory.getProfileAdminId(); adminUser = TestFactory.createUser('FirstAdmin',admProfId); insert adminUser; System.runAs(adminUser){ //create the Custom Settings ServiceTokens__c servToken = new ServiceTokens__c( Name = 'ProjectServiceToken' ,Token__c = 'DUMMY-TOKEN'); insert servToken; //create account Account acc = TestFactory.createAccount('Account 1'); insert acc; //create oppty oppLst = new List<Opportunity>{ TestFactory.createOpportunity('Project1',acc.Id, 'New project' ,'Submitted Project', Date.newInstance(2019, 11, 17), 17000), TestFactory.createOpportunity('Project2',acc.Id, 'New project' ,'Resubmit Project', Date.newInstance(2019, 12, 17), 17000) }; insert oppLst; } } @isTest static void testCalloutSuccess() { System.runAs(adminUser){ Test.startTest(); // Set mock callout class Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMock()); ProjectCalloutService.postOpportunityToPMS(new List<Id>{oppLst[0].Id}); Test.stopTest(); Opportunity opp = [select StageName from Opportunity where id =:oppLst[0].Id]; System.assertEquals('Submitted Project',opp.StageName); } } @isTest static void testCalloutFailure() { System.runAs(adminUser){ Test.startTest(); // Set mock callout class Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMockFailure()); ProjectCalloutService.postOpportunityToPMS(new List<Id>{oppLst[1].Id}); Test.stopTest(); Opportunity opp = [select StageName from Opportunity where id =: oppLst[1].Id]; System.assertEquals('Resubmit Project',opp.StageName); } } }
When running the above test class, both test method fail with uncommitted work callout exception below:
System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
However, if only the lines of code for test user creation and system.runas is commented then the test class executes successfully as the running user.
It is recommended to run test methods as a testing user, but then how do we insert the test user in the test class and prevent the uncommitted work error ?
This known issue is similar but is marked as fixed since a long time, I am wondering if the issue still persists.
Any help is appreciated, thanks