I am trying to write a test for a trigger that is firing a future apex callout :

The trigger calls the callBillingService method from this class:

public class BillingCalloutService {
    //Implement business and callout logic methods here
    public static void callBillingService(List<Id> recordIds) {
        List<Project__c> projects = [Select Id, ProjectRef__c,Billable_Amount__c from Project__c Where Id = :recordIds];

        List<Project__c> projectsToUpdate = new List<Project__c>();

        String username = ServiceCredentials__c.getValues('BillingServiceCredential').Username__c;
        String password = ServiceCredentials__c.getValues('BillingServiceCredential').Password__c;

        String auth = username + ':' + password;
        String encodedAuth = EncodingUtil.base64Encode(Blob.valueOf(auth));

        for(Project__c p : projects) {

            BillingServiceProxy.InvoicesPortSoap11 service = new BillingServiceProxy.InvoicesPortSoap11();
            service.inputHttpHeaders_x = new Map<String, String>();
            service.inputHttpHeaders_x.put('Authorization', 'Basic ' + encodedAuth);

            BillingServiceProxy.project project = new BillingServiceProxy.project();
            project.username = username;
            project.password = password;
            project.projectRef = p.ProjectRef__c;
            project.billAmount = p.Billable_Amount__c;

                projectsToUpdate.add(new Project__c(ProjectRef__c = p.ProjectRef__c, Status__c = 'Billed'));
        upsert projectsToUpdate ProjectRef__c;


Methods defined as TestMethod do not support Web service callouts

Test Class

private class BillingCalloutServiceTest {

    private static void testBillingCalloutService() {
    Account a = new Account(Name = 'Acme');
    insert a;
    Opportunity o = new Opportunity(Name='Test', StageName='Submitted Project', AccountId=a.Id, Amount=1000, CloseDate=Date.Today());
    insert o;
    Project__c p = new Project__c(Status__c='Running',Start_Date__c=Date.Today(),End_Date__c=Date.Today(),Billable_Amount__c=10000,ProjectRef__c='projectX',Opportunity__c=o.Id);
    insert p;

    insert new ServiceCredentials__c(Name='BillingServiceCredential',Username__c='toto', Password__c='azerty');

    Test.setMock(HttpCalloutMock.class, new BillingCalloutServiceMock());
      p.Status__c = 'Billable';
      update p;
    // runs callout and check results
    p = [select Status__c from Project__c where id =: p.id];
    System.assertEquals('Billed', p.Status__c);

  • You cannot call webservice from test class. Either you need to Mock api callout or include Test.isrunningtest() in your class.
    – Reshma
    Feb 7, 2017 at 11:34
  • At what point is callBillingService() called? Only on update of Product__c? Or also on Insert? Also, is it possible there are any other callouts being made?
    – Bob Lopez
    Feb 7, 2017 at 17:49
  • it is called in the project update trigger, it is the only entry point
    – EMG
    Feb 7, 2017 at 18:09

1 Answer 1


You need to make sure you set the mock within the context. If you call Test.startTest() in between setting your mock and making the callout, it won't work.


    // make callout


    // make callout
  • 3
    I have working code following your "Error" pattern so don't think this is the cause of the OP's problem.
    – Keith C
    Feb 7, 2017 at 14:13
  • Hmm, maybe it's due to the asynchronous nature of the call. Might have to turn the callout off during testing and then test the @future method directly. Don't have time to verify at the moment.
    – Adrian Larson
    Feb 7, 2017 at 14:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .