I invoking webservice callout from trigger and getting the error when running test
ERROR:
System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
Stack Trace: Class.IBPortal_LinkMT4Account.Link_MT4AccountPort.Link_Account: line 44, column 1 Class.IBPortal_MT4LinkerWS.Link_MT4AC: line 9, column 1
TEST CLASS CODE:
@isTest
private class ADSS_CP_WS_Test {
@isTest static void WithdrawalRequests1 () {
// This causes a fake response to be generated
Date myDate = Date.today();
Account act= new Account(
Name='test',
Lead_Source__c='Advertisement');
insert act;
ADSS_Platform_Account__c ADPA = new ADSS_Platform_Account__c(
Name='test',
Prefered_Currency__c='USD',
Instance__c='default',
Trading_Platform__c='MT4',
Account__c=act.id,
Client_Opening_Date__c=myDate);
insert ADPA;
***ONE CALLOUT IS INVOKED HERE , HOW TO TEST THIS***
ADSS_Platform_Account_Withdrawal_Fundig__c ad = new ADSS_Platform_Account_Withdrawal_Fundig__c(
Amount__c=1000,
Platform_Transaction_comments__c='test',
Request_Type__c='Charges',
Client_Portal_Post_Comment__c='test',
CCY__c='USD',
ADSS_Platform_Account__c=ADPA.id,
Initial_Deposit__c=false,
Post_Transaction__c=true,
Account_Name__c= act.id);
insert ad;
Test.startTest();
Test.setMock(WebServiceMock.class, new ADSSWebServicesMockImpl());
Test.stopTest();
}
TRIGGER:
trigger trg_ClientPortal_Funds_Withd on ADSS_Platform_Account_Withdrawal_Fundig__c (after insert) {
Set<Id> wreq= new Set<Id>();
Set<Id> freq= new Set<Id>();
Boolean callWS=false;
Boolean callWSf=false;
for (ADSS_Platform_Account_Withdrawal_Fundig__c withd : trigger.new) {
if (withd.Post_Transaction__c == TRUE && (withd.Request_Type__c == 'Withdrawal')||(withd.Request_Type__c == 'Charges')){
wreq.add(withd.Id);
callWS=true;
}
if (withd.Post_Transaction__c == TRUE && withd.Request_Type__c == 'Funding') {
freq.add(withd.Id);
callWSf=true;
}
}
if (callWS)
ADSS_CP_WS.WithdrawalRequests(wreq);
if (callWSf){
system.debug('funding');
ADSS_CP_WS.DepositRequests(freq);
}
}
We are making callout after inserting record of ADSS_Platform_Account__c object from class from trigger:
public class IBPortal_MT4LinkerWS {
@Future(callout=true)
public static void Link_MT4AC(Set<Id> PlatformIds){
Account ac;
for (ADSS_Platform_Account__c ld: [select id, Name, Account__c,IB_ID__c,Instance__c from ADSS_Platform_Account__c where id in:PlatformIds]){
ac = [select IBPortalID__c,SF_LeadID__c from Account where id=:ld.Account__c];
System.debug('--> IBPortal_MT4LinkerWS');
IBPortal_LinkMT4Account.Link_MT4AccountPort stub= new IBPortal_LinkMT4Account.Link_MT4AccountPort();
String output = stub.Link_Account(String.valueof(ac.IBPortalID__c),ld.Name,ac.id,ld.Instance__c,ld.IB_ID__c);
System.debug('--> Link_MT4AC '+output);
}
}
}
Mock response:
global class ADSSWebServicesMockImpl 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) {
System.debug('Print stub '+stub);
if (stub instanceof IBPortal_LinkMT4Account.Link_MT4AccountPort)
{
IBPortal_LinkMT4Account.Link_AccountResponse respElement =
new IBPortal_LinkMT4Account.Link_AccountResponse();
respElement.return_x= 'Linked';
response.put('response_x', respElement);
}
if(stub instanceof AdssClientportalWS.ClientServiceSoap11) {
else if(request instanceof AdssClientportalWS.WithdrawalRequest) {
AdssClientportalWS.WithdrawalRequest rRequest = (AdssClientportalWS.WithdrawalRequest)request;
AdssClientportalWS.WithdrawalResponse resp = new AdssClientportalWS.WithdrawalResponse();
if(rRequest.amount>50){
resp.success = true;
resp.errorCode= 'ok';
}
else {
resp.success = false;
resp.errorCode= 'failed';
}
//resp.lastTrades = new List<comAdssClientportalSfapi.Trade> {new comAdssClientportalSfapi.Trade()};
response.put('response_x', resp);
}
}
}
How to write test class for this scenario? what modifications are required?