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
jhennyjhenny 

setMock throws callout exception

Seems like the new setMock test methods don't like when you have DML that creates your test data prior to doing your mock callouts. I.e.:

 

 

    @isTest static void TestCallout() {              
   
    //  Test data   
        Account a = new Account(Name = 'test', BillingStreet='test', BillingCountry='United States',BillingCity='test',BillingState='California');
        insert a; 
   		
        // This causes a fake response to be generated     
        Test.setMock(WebServiceMock.class, new MyCalloutClass());
        
        // Call the method that invokes a callout 
    	MyCalloutClass p = new MyCalloutClass();
        String output = p.DoCallout();
        
        // Verify that a fake result is returned     
        System.assertEquals('Mock response', output); 
    }

 

I get the error "You have uncommitted work pending. Please commit or rollback before calling out"

 

I guess the workaround is maybe to use loadData, but seems strange that this error would appear in a test method

sfdcfoxsfdcfox

That does seem somewhat odd. Keep in mind though, that you can never callout after a DML, so it's consistent with current logic. Set your mock first, then callout.

BijayBijay

Hi ,

 

I have parse one WSDl for createAccount Relationshipship as per the Apex Develoiper guide ,

i was trying to test by providing a mock reponse

global class YourWebServiceMockImpl 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) {
// Create response element from the autogenerated class.
// Populate response element.
// Add response element to the response parameter, as follows:
response.put('response_x', responseElement);
}
}

 

and test class would be in this format

 

@isTest
private class WebSvcCalloutTest {
@isTest static void testEchoString() {
// This causes a fake response to be generated
Test.setMock(WebServiceMock.class, new WebServiceMockImpl());
// Call the method that invokes a callout
String output = WebSvcCallout.callEchoString('Hello World!');
// Verify that a fake result is returned
System.assertEquals('Mock response', output);
}
}

 

But according to my WSDL i dnt have any response element in the response wrapper class,so how to test my Apex class which call out the WSDl class.

 

Please reply its urgent and i am stuck.

Thanks in Advance.

BijayBijay

Hi jhenny,

I have parsed one WSDl for createAccount Relationshipship as per the Apex Develoiper guide ,

i was trying to test by providing a mock reponse

global class YourWebServiceMockImpl 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) {
// Create response element from the autogenerated class.
// Populate response element.
// Add response element to the response parameter, as follows:
response.put('response_x', responseElement);
}
}

 

and test class would be in this format

 

@isTest
private class WebSvcCalloutTest {
@isTest static void testEchoString() {
// This causes a fake response to be generated
Test.setMock(WebServiceMock.class, new WebServiceMockImpl());
// Call the method that invokes a callout
String output = WebSvcCallout.callEchoString('Hello World!');
// Verify that a fake result is returned
System.assertEquals('Mock response', output);
}
}

 

But according to my WSDL i dnt have any response element in the response wrapper class,so how to test my Apex class which call out the WSDl class.

 

Please reply its urgent and i am stuck.

Thanks in Advance.