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
JacobusJacobus 

Test @future call from trigger

Hi,

I have the below trigger that makes an @future call to an apex class.
This line  UpliftmentRequestFutureWebService.sendvehiclerequest makes the call.

I have tried various things in my test class such as test.startTest() and test.stopTest() and still fails with a "Methods defined as TestMethod do not support Web service callouts, test skipped".

Any guidance is very welcome on this!


trigger UpliftmentRequestCreate on Vehicle__c (after insert) {
    
    DateTime createdate = system.now();
    String dateofrequest;
    Integer insurer;
    String claimno;
    DateTime dateoflossconvert;
    String dateofloss;
    Integer vehiclecondition;
    String customerfirstname;
    String customerlastname;
    String vehiclemake;
    String vehiclemodel;
    String vehiclemmcode;
    String vehicleyear;
    String vehicleregistrationno;
    String vehiclecommercialpassenger;
    String locationname;
    String locationcontactperson;
    String locationcontactno;
    String locationemailaddress;
    String locationstreetaddress;
    String locationsuburb;
    String locationpostalcode;
    String locationcity;
    String locationprovince;
    String createuserfirstname;
    String createuserlastname;
    String salesforcecapturer;
    String salesforcerefno;
    String refno;
    id customer;
    id vehicleid;
        
    User currentuser=[Select FirstName, LastName from User Where Id =: UserInfo.getUserId() LIMIT 1];
    createuserfirstname = currentuser.FirstName;
    createuserlastname = currentuser.LastName;
    salesforcecapturer = createuserfirstname+' '+createuserlastname;
    system.debug ('Current User : ' + createuserfirstname +' '+createuserlastname);

    for (Vehicle__c v : Trigger.new) {
        vehicleid = v.id;
        customer = v.Customer__c;
        //
        //DateOfRequest    
        if (v.Insurance_Group__c == 'AFI' || v.Insurance_Group__c == 'Zurich'){
            
        dateofrequest = createdate.format('yyyy/MM/dd HH:mm:ss');
        system.debug ('Date of Request New : ' + dateofrequest);                               
        //
        //Insurer
        if (v.Insurance_Group__c != null){
                if (v.Insurance_Group__c == 'AFI'){
                    insurer = 26;
                }
                
                else if (v.Insurance_Group__c == 'Zurich'){
                    insurer = 347;
                }
                else{
                    insurer = 1;
                }
        }
        else{
                insurer = 0;
        }
        system.debug('Insurer : ' + insurer);
        //
        //ClaimNumber                
        if (v.PolicyNo__c != null){
                claimno = v.PolicyNo__c;
        }
        else{
                claimno = 'Not Specified';
        }
        system.debug('Claim No : ' + claimno);
        //
        //DateOfLoss    
        if (v.Date_Of_Loss__c != null){
                
                dateoflossconvert = DateTime.newInstance(v.Date_Of_Loss__c.year(), v.Date_Of_Loss__c.month(), v.Date_Of_Loss__c.day());
                dateofloss = dateoflossconvert.format('yyyy/MM/dd HH:mm:ss');
        }
        else{
                dateofloss = null;
        }        
        system.debug('Date of Loss : ' + dateofloss);
        //
        //VehicleCondition        
        if(v.Vehicle_Condition__c != null){
                if (v.Vehicle_Condition__c == 'Repairable'){
                    vehiclecondition = 1;
                }
                else if (v.Vehicle_Condition__c == 'WriteOff'){
                    vehiclecondition = 2;
                }
                else{
                    vehiclecondition = 0;
                }
        }
        else{
                vehiclecondition = 0;
        }
        system.debug('Vehicle Condition : ' + vehiclecondition);
        //
        //VehicleCommercialPassenger
        if(v.Vehicle_Commercial_Passenger__c != null){
                vehiclecommercialpassenger = v.Vehicle_Commercial_Passenger__c;
        }
        else{
                vehiclecommercialpassenger = 'Not Specified';
        }
        system.debug ('Vehicle Commercial Passenger : ' + vehiclecommercialpassenger);
        //
        //VehicleMake
        if(v.Make__c != null){
            vehiclemake = v.Make__c;
        }    
        else{
            vehiclemake = 'Not Specified';
        }
        system.debug ('Vehicle Make : ' + vehiclemake);
        //
        //VehicleModel
        if (v.Model__c != null){
                vehiclemodel = v.Model__c;
        }        
        else{
                vehiclemodel = 'Not Specified';
        }
        system.debug('Vehicle Model : ' + vehiclemodel);
        //
        //VehicleMMCode
        if (v.MMCode__c != null){
            vehiclemmcode = v.MMCode__c;
        }
        else{
            vehiclemmcode = 'Not Specified';
        }
        system.debug ('Vehicle MMCode : ' + vehiclemmcode);
        //
        //VehicleYear
        if (v.Reg_Year__c != null){
                vehicleyear = v.Reg_Year__c;    
        }
        else{
                vehicleyear = 'Not Specified';
        }
        system.debug('Vehicle Year : ' + vehicleyear);
        //
        //VehicleRegistrationNumber
        if(v.RegNo__c != null){
                vehicleregistrationno = v.RegNo__c;    
        }
        else{
                vehicleregistrationno = 'Not Specified';
        }
        system.debug('Vehicle Registration No : ' + vehicleregistrationno);
        //
        //LocationName
        if (v.MBR_Name__c != null){
                locationname = v.MBR_Name__c;    
        }
        else {
                locationname = 'Not Specified';
        }
        system.debug('Location Name : ' + locationname);
        //
        //LocationContactPerson    
        if (v.MBR_Contact_Person__c != null){
                locationcontactperson = v.MBR_Contact_Person__c;    
        }
        else{
                locationcontactperson = 'Not Specified';
        }
        system.debug('Location Contact Person : ' + locationcontactperson);
        //
        //LocationEmailAddress    
        if (v.MBR_Email__c != null) {
                locationemailaddress = v.MBR_Email__c;    
        }
        else {
                locationemailaddress = 'Not Specified';
        }
        system.debug('Location Email Address : ' + locationemailaddress);
        //
        //LocationStreetAddress    
        if (v.MBR_Address__c != null){
                locationstreetaddress = v.MBR_Address__c;    
        }
        else {
                locationstreetaddress = 'Not Specified';
        }
        system.debug('Location Address : ' + locationstreetaddress);
        //
        //LocationSuburb    
        if (v.MBR_Suburb__c != null){
                locationsuburb = v.MBR_Suburb__c;    
        }
        else{
                locationsuburb = 'Not Specified';
        }
        system.debug('Location Suburb : ' + locationsuburb);
        //
        //LocationPostalCode
        if (v.MBR_Postal_Code__c != null){
            locationpostalcode = v.MBR_Postal_Code__c;
        }
        else{
            locationpostalcode = '';
        }
        //
        //LocationCity
        if (v.MBR_City__c != null){
            locationcity = v.MBR_City__c;
        }
        else{
            locationcity = '';
        }
        //
        //LocationProvince    
        if (v.MBR_Province__c != null){
                locationprovince = v.MBR_Province__c;    
        }
        else {
                locationprovince = 'Not Specified';
        }        
        system.debug('Location Province : ' + locationprovince);    
        //
        //LocationContactNumber    
        if (v.MBR_Contact_No__c != null){
                locationcontactno = v.MBR_Contact_No__c;
        }
        else{
                locationcontactno = 'Not Specified';
        }
        system.debug('Location Province : ' + locationcontactno);
        //
        //SalesforceReferenceNumber    
        if (v.StockNo__c != null){
                salesforcerefno = v.StockNo__c;    
        }
        else{
                salesforcerefno = 'Not Specified';
        }
        //
        }    
    }
    
    for (Account a : [Select Id, FirstName, LastName from Account where Id =: customer LIMIT 1]) {
        
        if (a.FirstName != null){
            customerfirstname = a.FirstName;    
        }
        else {
            customerfirstname = 'Not Specified';
        }
        
        if (a.LastName != null){
            customerlastname = a.LastName;    
        }
        else{
            customerlastname = 'Not Specified';
        }
        system.debug('Customer Name : ' + customerfirstname +' '+ customerlastname);
    }
    
        
    for (Vehicle__c v : Trigger.new) {
        
        if (v.Insurance_Group__c == 'AFI' || v.Insurance_Group__c == 'Zurich'){ 
    
                try {
                
                system.debug ('About to call the web service class');
                UpliftmentRequestFutureWebService.sendvehiclerequest(dateofrequest, insurer, claimno, dateofloss, vehiclecondition, customerlastname, customerfirstname, vehiclecommercialpassenger, vehiclemake, vehiclemodel, 
                                                                     vehicleMMCode, vehicleyear, vehicleregistrationno, locationname, locationcontactperson, locationcontactno, locationemailaddress, locationstreetaddress, 
                                                                     locationsuburb, locationpostalcode, locationcity, locationprovince, salesforcecapturer, salesforcerefno);        
                }
                    catch(DmlException e){
                    system.debug('The following exception has occured: ' + e.getMessage());
                }
        }
    }            
}
PrakashbPrakashb
Hi,

You cannot make callouts from test classes.

Please refer the below link for creating a mockup call.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex_testing.htm

Regards,
Prakash B
JacobusJacobus
Hi Prakash B,

Thank you for your reply.

This is my code what I tried, but when testing them, I get a "File name mismatch with class name: MyWebServiceMock".
Appreciate your help!

@isTest

global class MyWebServiceMock implements WebServiceMock {
   global void doInvoke(
           Object stub,
           Object request,
           Map<String, Object> response,
           String endpoint,
           String soapAction,
           String requestName,
           String responseNS,
           String responseName,
           String responseType) {
                 
              UpliftmentRequestWSDL.SumbitReleaseFromThirdPartyResponse_element respElement = new UpliftmentRequestWSDL.SumbitReleaseFromThirdPartyResponse_element();
              respElement.SumbitReleaseFromThirdPartyResult = 'Mock response';
           response.put('response_x', respElement); 
             
           }  
}

@isTest(SeeAllData=true)
private class UpliftmentRequestWSDLTest {

    static void testWSDL() {      
                  

        // This causes a fake response to be generated
        
        FAMExternalWebservice.ThirdPartyReleaseEntry tpr = new FAMExternalWebservice.ThirdPartyReleaseEntry();    
        FAMExternalWebservice.SumbitReleaseFromThirdParty stub = new FAMExternalWebservice.SumbitReleaseFromThirdParty();    
        
                tpr.DateOfRequest = 'system.now()';
                tpr.Insurer = 1;
                tpr.ClaimNumber = 'claimno';
                tpr.DateOfLoss = 'system.now()';
                tpr.VehicleCondition = 1;
                tpr.CustomerSurname = 'customerlastname';
                tpr.CustomerName = 'customerfirstname';
                tpr.VehicleCommercialPassenger = 'vehiclecommercialpassenger';                
                tpr.VehicleMake = 'vehiclemake';
                tpr.VehicleModel = 'vehiclemodel';
                tpr.VehicleMMCode = 'vehicleMMCode';
                tpr.VehicleYear = 'vehicleyear';
                tpr.VehicleRegistrationNumber = 'vehicleregistrationno';
                tpr.LocationName = 'locationname';
                tpr.LocationContactPerson = 'locationcontactperson';
                tpr.LocationContactNumber = 'locationcontactno';
                tpr.LocationEmailAddress = 'locationemailaddress';
                tpr.LocationStreetAddress = 'locationstreetaddress';
                tpr.LocationSuburb = 'locationsuburb';
                tpr.LocationPostalCode = 'locationpostalcode';
                tpr.LocationCity = 'locationcity';
                tpr.LocationProvince = 'locationprovince';
                tpr.SalesForceCapturer = 'salesforcecapturer';
                tpr.SalesForceReferenceNumber = 'salesforcerefno';    
                
        Test.StartTest() ;
        Test.setMock(WebServiceMock.class, new MyWebServiceMock());

        // Call the method that invokes a callout
        String output = stub.SumbitReleaseFromThirdParty(tpr);
        Test.StopTest();
         
        // Verify that a fake result is returned
        System.assertEquals('Mock response', output);

    }
}