I am getting a "you have uncommitted work pending" error on a test. Normally this is caused by a DML statement before a callout. That shouldn't be the case here. However, the code runs fine and I am using Test.start and stop appropriately. Im hoping someone can see what I'm missing.
Here's the class:
//Scheduler public void execute(SchedulableContext sc) { DoseSpotRetrieval dsr = new DoseSpotRetrieval(); Database.executebatch(dsr, 30); } //Loop over accounts (in batches of 30 so calls dont exceed 100) public Database.QueryLocator start(Database.BatchableContext BC){ String query = 'SELECT DoseSpot_Id__c FROM Account WHERE DoseSpot_Id__c != null'; return Database.getQueryLocator(query); } //Handle actual logic public void execute(Database.BatchableContext BC, List<Account> accounts){ String doseSpotSystemUser = Label.DoseSpot_System_User; String authToken = DoseSpotCallout.GetAuthToken(doseSpotSystemUser); getMetadata(); //This is just SOQL List<HealthCloudGA__EhrAllergyIntolerance__c> hcAllergies = new List<HealthCloudGA__EhrAllergyIntolerance__c>(); List<HealthCloudGA__EhrMedicationPrescription__c> hcMedications = new List<HealthCloudGA__EhrMedicationPrescription__c>(); //Check setup data if(String.isBlank(authToken)) { System.debug('Failed to get auth token'); ErrorUtility.CreateSystemLog('DoseSpotRetrieval','GetAuthToken',null,'Failed to get auth token', true); } for(Account a : accounts) { String patientId = a.DoseSpot_Id__c; //Get allergies (Documentation section: 3.1.2), convert to records to hcRecords String urlEndAllergies = '/api/patients/' + patientId + '/allergies'; List<Map<String,Object>> allergies = getDoseSpotData(authToken, urlEndAllergies); //CALLOUT HAPPENS IN THIS METHOD. ITS JUST A CALLOUT, NO DML ... ... ... Here's the test class
@TestSetup static void makeData(){ SObjectType sobjType = Schema.getGlobalDescribe().get('Account'); Map<String, Schema.RecordTypeInfo> acctRTs = sobjType.getDescribe().getRecordTypeInfosByDeveloperName(); //Insert patient record Account patient = new Account( Account_Key__c = '123456-09876', FirstName = 'FirstName', LastName = 'LastName', RecordTypeId = acctRTs.get('Sexual_Health_Patient').getRecordTypeId(), DoseSpot_Id__c = '12345' ); insert patient; } @isTest static void testHappyPath() { Test.setMock(HttpCalloutMock.class, new MockResponse(0)); Test.startTest(); DoseSpotRetrieval dsr = new DoseSpotRetrieval(); dsr.execute(null); Test.stopTest(); List<HealthCloudGA__EhrAllergyIntolerance__c> allergies = [SELECT HealthCloudGA__Account__c, DoseSpot_Id__c FROM HealthCloudGA__EhrAllergyIntolerance__c]; List<HealthCloudGA__EhrMedicationPrescription__c> meds = [SELECT HealthCloudGA__Account__c, DoseSpot_Id__c FROM HealthCloudGA__EhrMedicationPrescription__c]; System.assertEquals(1, allergies.size()); System.assertEquals('1', allergies[0].DoseSpot_Id__c); System.assert(allergies[0].HealthCloudGA__Account__c != null, 'Account was not populated on allergy'); System.assertEquals(2, meds.size()); for(HealthCloudGA__EhrMedicationPrescription__c med : meds) { System.assertEquals('1', med.DoseSpot_Id__c); System.assert(med.HealthCloudGA__Account__c != null, 'Account was not populated on med'); } }