Skip to main content

It seems that System.Formula.recalculateFormulas method triggers unexpected behaviour during apex unit tests. Has anyone faced this issue or has any explanation why this is happening? Code example below:

public class CalloutsForTest {

final Account account;

CalloutsForTest(Id accountId) {

this.account = [SELECT Id FROM Account WHERE Id = :accountId];

}

public static void doStatelessCallout(Id accountId){

CalloutsForTest calloutObject = new CalloutsForTest(accountId);

calloutObject.doStatefulCallout();

}

public void doStatefulCallout() {

//If recalculateFormulas method (17-21 lines) is recalculated, then test does not fail

{

System.Formula.recalculateFormulas(new List<Account>{

account

});

}

Http http = new Http();

HttpRequest request = new HttpRequest();

request.setMethod('GET');

request.setEndpoint('https://www.google.com/');

HttpResponse response;

response = http.send(request);

if (response.getStatusCode() != 200) {

throw new CalloutException(response.getBody());

}

}

}

 

@IsTest

private class CalloutTest {

@IsTest

static void test() {

//If we remove test data preparation DML formula recalculation method do not break unit test

Id accountId = Database.insert(new Account(Name = 'Test inc.')).getId();

Test.startTest();

Test.setMock(HttpCalloutMock.class, new HttpMock());

CalloutsForTest.doStatelessCallout(accountId);

Test.stopTest();

System.assert(true);

}

public class HttpMock implements HttpCalloutMock {

public HttpResponse respond(HttpRequest request) {

HttpResponse response = new HttpResponse();

response.setStatusCode(200);

return response;

}

}

}

0/9000