1

I've created a trigger with fields that are now in a web service callout. I've created this trigger and deployed in my org before the callout.

So now I'm getting an error: "Methods defined as TestMethod do not support Web service callouts, test skipped"

This error appears in the test class that is covering this trigger in specific.

And always when I will look for something related to this error. I found an answer that is related to web service callout, but my error is in a trigger that only have the same fields and not exactly in the web service callout.

Summarizing, I need to now how to create a test class for my trigger that have fields in a web service callout.

My trigger:

trigger AtualizarDataSeparacaoAvulso_case on Account (after update) {

    Set <id> ids = new Set <id> ();
        for (Account acc: trigger.new) {
            if (Trigger.oldMap.get(acc.id).Data_separa_o_avulso__c != acc.Data_separa_o_avulso__c || Trigger.oldMap.get(acc.id).Data_lab_avulso__c != acc.Data_lab_avulso__c){
                ids.add(acc.id);
            }
        }
        for (Case c: [SELECT Id, motivo__c, Data_lab_avulso__c, Data_separa_o_avulso__c, Fotos_Colacao__c, Fotos_Baile__c, Fotos_Outros__c, Panoramicas__c FROM Case WHERE motivo__c = 'Impressão de álbum' AND accountid IN: ids LIMIT 1]){

        for (Account acc: trigger.new) {

            if(acc.Data_separa_o_avulso__c == null && Trigger.oldMap.get(acc.id).Data_separa_o_avulso__c != acc.Data_separa_o_avulso__c){        
                c.Data_separa_o_avulso__c = null;
            }
            if(acc.Data_separa_o_avulso__c != null && Trigger.oldMap.get(acc.id).Data_separa_o_avulso__c != acc.Data_separa_o_avulso__c){       
                c.Data_separa_o_avulso__c= acc.Data_separa_o_avulso__c;
                c.Fotos_Colacao__c= acc.Fotos_Colacao__c;
                c.Fotos_Baile__c=acc.Fotos_Baile__c;
                c.Fotos_Outros__c=acc.Fotos_Outros__c;
                c.Panoramicas__c=acc.Panoramicas__c;
            }
            if(acc.Data_lab_avulso__c == null && Trigger.oldMap.get(acc.id).Data_lab_avulso__c != acc.Data_lab_avulso__c){        
                c.Data_lab_avulso__c = null;
            }
            if(acc.Data_lab_avulso__c != null && Trigger.oldMap.get(acc.id).Data_lab_avulso__c != acc.Data_lab_avulso__c){       
                c.Data_lab_avulso__c= acc.Data_lab_avulso__c;
            }
    update c;
    }
    }
    }
  • Can you explain "with fields that are now in a web service callout" more? – Keith C Jun 25 '14 at 13:49
1

If you are getting that error then somehow or other a web service callout is being made...

If the web service callout has absolutely nothing to do with the code your are testing you could skip the web service callout when tests are running by including a check of:

if (!Test.isRunningTest()) {
    // Only make web service call when not running tests
}

in the web service callout code. Or you could introduce a static Boolean flag and use that to turn off the web service callout for specific tests:

if (!SomeClass.TurnOffWebServiceCallForTests) {
    // Only make web service call when flag not set
}

But the more complete approach is that when code that your test causes to run makes a web service callout, you simulate that callout by implementing a mock class. The details depend on the type of callout your are doing:

In both cases, after creating the implementation class of the required interface, you register an instance of it with the testing framework using Test.setMock.

While this is all quite a lot of work it has two benefits:

  • It allows you to tie down the responses that your test assertions will work against and so makes your tests repeatable
  • It allows Salesforce to run the tests without expensive web service callouts occurring
  • I just add? @isTest(SeeAllData=true) public class AtualizarDataSeparacaoAvulso_test{ static testmethod void MyUnitTest() { if (!Test.isRunningTest()) { – user8553 Jun 25 '14 at 14:44
  • @user8553 You have to add the if (!Test.isRunningTest()) { around the code that makes the web service callout to not do it. Generally its best to avoid @isTest(SeeAllData=true); instead tests should add all the data they need. That is an unrelated issue. – Keith C Jun 25 '14 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.