function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Steve CairneySteve Cairney 

Controller test class is invoking Web Service Callout - how to stop?

HI, I'm working on the follwoing controller class test for a VF page. I've no idea if it works yet but when I run a test I get the following error.
TestMethod do not support Web service callouts

I belive this is because there is another trigger on the system that first when one of the objects  in the below test class is created. I don't want to remove this trigger, but I'd like somehow to edit my code below so that it doesn't throw up the error. I've read about mock calls but I'm not sure where the code would sit in my class. Any help would be appreiciated
public class CreateIABookingControllerTest{
    public static testMethod void myTest() {
    PageReference pageRef = Page.CreateIABookingPage;
    Test.setCurrentPage (pageRef);
    CreateIABookingController controller = new CreateIABookingController();
    	//Set up test Account
        Account testaccount = new Account (Name='test account');
        insert testaccount;
        //Set up test Opp
        Opportunity testopp = new Opportunity(AccountId=testaccount.Id, Name = 'test opp', StageName = 'Closed Won', CloseDate =;
        insert testopp;
        //Set up test IABooking
        ItsApproved_Booking__c testiabooking = new ItsApproved_Booking__c(Opportunity__c=testopp.Id);
        insert testiabooking;
        //Set up test IAProduct
        ItsApproved_Product__c testiaproduct = new ItsApproved_Product__c(Booking_Reference__c=testiabooking.Id,Opp__c=testopp.Id);
        insert testiaproduct;
        //Set up test IADesign
        ItsApproved_Design__c testiadesign = new ItsApproved_Design__c(Product_Reference__c=testiaproduct.Id,Opportunity__c=testopp.Id);
        insert testiadesign;
        //Set up test IADelivery
        ItsApproved_Delivery__c testiadelv = new ItsApproved_Delivery__c(ItsApproved_Design__c=testiadesign.Id,Opportunity__c=testopp.Id,Product__c=testiaproduct.Id);
        insert testiadelv;
    	controller = new CreateIABookingController();
    	//start test - Create Booking
    	controller.ItsApprovedBooking.Opportunity__c = testopp.Id;//related Opp
        controller.ItsApprovedBooking.Booking_Title__c = 'Booking Title';
        controller.ItsApprovedBooking.Customer_Reference__c = 'Customer Ref';
    	insert controller.ItsApprovedBooking;
        //Add Product to Booking
        controller.ItsApprovedProduct.Booking_Reference__c = testiabooking.Id;//related Booking
        controller.ItsApprovedProduct.Opp__c = testopp.Id;//related Opp
        controller.ItsApprovedProduct.Product_Code__c = 'Static 6s';
        insert controller.ItsApprovedProduct;
        //Add Design to Product
        controller.ItsApprovedDesign.Product_Reference__c = testiaproduct.Id; //related Product
        controller.ItsApprovedDesign.Opportunity__c = testopp.Id; //related Opp
        controller.ItsApprovedDesign.Artwork_Title__c = 'Artwork Title';
        insert controller.ItsApprovedDesign;
        //Add Delivery to Design
        controller.ItsApprovedDelivery.ItsApproved_Design__c = testiadesign.Id; //related Design
        controller.ItsApprovedDelivery.Product__c = testiaproduct.Id; //related Product
        controller.ItsApprovedDelivery.Opportunity__c = testopp.Id; //related Opp
        controller.ItsApprovedDelivery.Depot_Code__c = integer.valueOf(123);
        controller.ItsApprovedDelivery.Quantity__c = integer.valueOf(10);
        insert controller.ItsApprovedDelivery;

You'll probably need to bracket the callout with:

if (Test.isRunningTest()) {

If you need data back from the call for testing, then you can add an else that provides dummy data.
Steve CairneySteve Cairney
This class actually doesn't need a Callout at all so would it be possible to add your if statement into the class above? If so where abouts would it go?
There must be a trigger that is performing a callout.  The trigger should be set up to handle the test scenario.