You need to sign in to do that
Don't have an account?
Ravi kumar 292
How to bypass the WebserviceCallout.Invoke
Hi all,
Help me how to bypass the webservice callout in a test class.
Code:
Help me how to bypass the webservice callout in a test class.
Code:
WebServiceCallout.invoke(
this,
request_x,
response_map_x,
new String[]{endpoint_x,
'/ CodeCoverage/ReturnData',
'http://www.abc.com/xyz/sampleSchema.xsd',
' Input_element',
'http://www.abc.com/xyz/sampleSchema.xsd',
‘ResponseElement’,
' ResponseElement'}
);
You can add like
if (!Test.isRunningTest()){
// invoke method
}
I am done with the same like code
If(!Test.isRunningTest()){
WebServiceCallout.Invoke(\
this,
request_x,
Object response_map_x,
new String[]{endpoint_x,
'http://www.mclsoftware.co.uk/HunterII/WebServices/Match',
'http://www.mclsoftware.co.uk/HunterII/WebServices',
'Match',
'http://www.mclsoftware.co.uk/HunterII/WebServices',
'MatchResponse',
'wwwMclsoftwareCoUkHunteriiWebservic.MatchResponse_element'});
}
I am getting the error as Variable does not exist: this.. Let me know to wat to do???
Thanks,,
'This' is a system variable and it should not errored out because fo 'If' condition, Can you paste your class here for further analysis? you can remove any sensitve info like userName, pwd, endpoint url and client certificate.
public class MatchService {
public class Match_element {
public String controlXml;
public String batchXml;
public String username;
public String password;
private String[] controlXml_type_info = new String[]{'controlXml','http://www.',null,'0','1','false'};
private String[] batchXml_type_info = new String[]{'batchXml','http://www.',null,'0','1','false'};
private String[] username_type_info = new String[]{'username','http://www.',null,'0','1','false'};
private String[] password_type_info = new String[]{'password','http://www.',null,'0','1','false'};
private String[] apex_schema_type_info = new String[]{'http://www.','true','false'};
private String[] field_order_type_info = new String[]{'controlXml','batchXml','username','password'};
}
public class MatchResponse_element {
public String MatchResult;
private String[] MatchResult_type_info = new String[]{'MatchResult','http://www.mclsoftware.co.uk/HunterII/WebServices',null,'0','1','false'};
private String[] apex_schema_type_info = new String[]{'http://www.mclsoftware.co.uk/HunterII/WebServices','true','false'};
private String[] field_order_type_info = new String[]{'MatchResult'};
}
public class MatchingSoap {
public String endpoint_x = 'https://www.test.com';
public Map<String,String> inputHttpHeaders_x;
public Map<String,String> outputHttpHeaders_x;
public String clientCertName_x = 'certificate';
public String clientCert_x;
public String clientCertPasswd_x = 'password';
public Integer timeout_x;
private String[] ns_map_type_info = new String[]{'http://www.', 'MatchService'};
public String Match(String controlXml,String batchXml,String username,String password) {
MatchService.Match_element request_x = new MatchService.Match_element();
request_x.controlXml = controlXml;
request_x.batchXml = batchXml;
request_x.username = username;
request_x.password = password;
wwwMclsoftwareCoUkHunteriiWebservic.MatchResponse_element response_x;
Map<String,MatchService.MatchResponse_element> response_map_x = new Map<String, MatchService.MatchResponse_element>();
response_map_x.put('response_x', response_x);
system.debug('batchXml:' +batchXml);
WebServiceCallout.invoke(
this,
request_x,
response_map_x,
new String[]{endpoint_x,
'http://www.test.com',
'http://www.test.com',
'Match',
'http://www.test.com',
'MatchResponse',
'MatchService.MatchResponse_element'}
);
response_x = response_map_x.get('response_x');
system.debug('response_x:' + response_x);
return response_x.MatchResult;
}
}
}
Help on this
public class MatchService {
public class Match_element {
public String controlXml;
public String batchXml;
public String username;
public String password;
private String[] controlXml_type_info = new String[]{'controlXml','http://www.',null,'0','1','false'};
private String[] batchXml_type_info = new String[]{'batchXml','http://www.',null,'0','1','false'};
private String[] username_type_info = new String[]{'username','http://www.',null,'0','1','false'};
private String[] password_type_info = new String[]{'password','http://www.',null,'0','1','false'};
private String[] apex_schema_type_info = new String[]{'http://www.','true','false'};
private String[] field_order_type_info = new String[]{'controlXml','batchXml','username','password'};
}
public class MatchResponse_element {
public String MatchResult;
private String[] MatchResult_type_info = new String[]{'MatchResult','http://www.mclsoftware.co.uk/HunterII/WebServices',null,'0','1','false'};
private String[] apex_schema_type_info = new String[]{'http://www.mclsoftware.co.uk/HunterII/WebServices','true','false'};
private String[] field_order_type_info = new String[]{'MatchResult'};
}
public class MatchingSoap {
public String endpoint_x = 'https://www.test.com';
public Map<String,String> inputHttpHeaders_x;
public Map<String,String> outputHttpHeaders_x;
public String clientCertName_x = 'certificate';
public String clientCert_x;
public String clientCertPasswd_x = 'password';
public Integer timeout_x;
private String[] ns_map_type_info = new String[]{'http://www.', 'MatchService'};
public String Match(String controlXml,String batchXml,String username,String password) {
MatchService.Match_element request_x = new MatchService.Match_element();
request_x.controlXml = controlXml;
request_x.batchXml = batchXml;
request_x.username = username;
request_x.password = password;
wwwMclsoftwareCoUkHunteriiWebservic.MatchResponse_element response_x;
Map<String,MatchService.MatchResponse_element> response_map_x = new Map<String, MatchService.MatchResponse_element>();
response_map_x.put('response_x', response_x);
system.debug('batchXml:' +batchXml);
if(!Test.isRunningTest()){
WebServiceCallout.invoke(
this,
request_x,
response_map_x,
new String[]{endpoint_x,
'http://www.test.com',
'http://www.test.com',
'Match',
'http://www.test.com',
'MatchResponse',
'MatchService.MatchResponse_element'}
);
}
response_x = response_map_x.get('response_x');
system.debug('response_x:' + response_x);
return response_x.MatchResult;
}
}
}
Let me tell you clearly..
I have two calsses and one WSDL2Apex class.
Here is the code.
Class @1:
public with sharing class A {
public string rid {get; set;}
public Opportunity o {get;set;}
public Account a {get; set;}
public Contact c {get;set;}
private ApexPages.StandardController standardController;
public A(ApexPages.StandardController standardController)
{
rid = ApexPages.CurrentPage().getparameters().get('id');
this.standardController = standardController;
try{
o=[Select id,Name,AccountId,Nationality__c,Purpose_of_Loan__c,Sanction_Amount__c,Application_Creation_Date__c,Original_Face__c,Loan_Tenor_in_Month__c,Other_Loan_Purpose__c from Opportunity where id= :rid];
a=[Select Id,Loan_Amount__c, Sector__c from Account where id = :o.AccountId];
c=[Select Id,FirstName,LastName,Birthdate,Borrower_Type__c,Name_of_Firm_Company_Establishment_In__c,Gender__c,Age1__c,PAN_ID__c,Residence_Type__c, Phone, Annual_Income_Gross__c,Organisation__c,MailingStreet, MailingCity,MailingState, MailingCountry,MailingPostalCode,Marital_Status__c,Email,MobilePhone From Contact where AccountId = :a.Id];
}catch(Exception e){
o = new Opportunity();
a = new Account();
c = new Contact();
}
}
public PageReference method(){
B H1 = new B(); // instantiate Class
H1.A(rid);
return null;
}
}
Class@2:
Public class B{
public void B(string rid){
Opportunity o=[Select id,Name,AccountId,Nationality__c,Purpose_of_Loan__c,Sanction_Amount__c,Application_Creation_Date__c,Original_Face__c,Loan_Tenor_in_Month__c,Other_Loan_Purpose__c from Opportunity where id= :rid];
system.debug('print me account id'+o.AccountId);
Account a=[Select Id,Loan_Amount__c, Sector__c from Account where id = :o.AccountId];
system.debug('Print me account id'+a.id);
List<Contact> c=[Select Id,FirstName,LastName,Birthdate,Borrower_Type__c,Name_of_Firm_Company_Establishment_In__c,Gender__c,Age1__c,PAN_ID__c,Residence_Type__c, Phone, Annual_Income_Gross__c,Organisation__c,MailingStreet, MailingCity,MailingState, MailingCountry,MailingPostalCode,Marital_Status__c,Email,MobilePhone From Contact where AccountId = :a.Id];
system.debug('Contact' + c);
contact Borrower = new Contact();
contact Coborrower= new Contact();
contact Guarantor = new Contact();
contact Patient = new Contact();
String dateInStringString;
String dateInStringString1;
String MM;
String DD;
String YYYY;
String AMM;
String ADD;
String AYYYY;
String result = '';
Integer i;
Integer Age;
Bank_Facilities__c bankfacility;
system.debug('bankfacility :' +bankfacility);
dateInStringString1 = string.valueOfGmt(o.Application_Creation_Date__c);
AMM = dateInStringString1.substring(5, 7);
system.debug('AMM :' +AMM);
ADD = dateInStringString1.substring(8, 10);
system.debug('ADD :' +ADD);
AYYYY = dateInStringString1.substring(0, 4);
system.debug('AYYYY :' +AYYYY);
if(o.Purpose_of_Loan__c== 'BUY FIRST HOUSE' || o.Purpose_of_Loan__c == 'LOCAL PURCHASE' || o.Purpose_of_Loan__c == 'BUY SMALL FARM' ||
o.Purpose_of_Loan__c == 'BUILDING' || o.Purpose_of_Loan__c == 'REFURBISHMENT PROPERTY' || o.Purpose_of_Loan__c == 'SUBROGATION' ||
o.Purpose_of_Loan__c == 'REST' || o.Purpose_of_Loan__c == 'NO INFORMAATION' || o.Purpose_of_Loan__c == 'CREDIT CARD')
{
o.Purpose_of_Loan__c = 'HOUSING LOAN';
} else
o.Purpose_of_Loan__c = 'OTHER PURCHASE';
for(i=0; i<c.Size();i++){
if (c[i].Borrower_Type__c == 'Borrower'){
Borrower.FirstName = c[i].FirstName;
Borrower.LastName = c[i].LastName;
Borrower.Birthdate = c[i].Birthdate;
Borrower.Gender__c = c[i].Gender__c;
Borrower.PAN_ID__c = c[i].PAN_ID__c;
// Borrower.Age1__c =c[i].Age1__c;
Borrower.phone = c[i].phone;
Borrower.Annual_Income_Gross__c = c[i].Annual_Income_Gross__c;
Borrower.Marital_Status__c =c[i].Marital_Status__c;
Borrower.Email = c[i].Email;
Borrower.MobilePhone = c[i].MobilePhone;
// Borrower.Sector__c = c[i].Sector__c;
Borrower.MailingStreet = c[i].MailingStreet;
Borrower.MailingCity = c[i].MailingCity;
Borrower.MailingState = c[i].MailingState;
Borrower.MailingCountry = c[i].MailingCountry;
Borrower.MailingPostalCode = c[i].MailingPostalCode;
Borrower.Residence_Type__c = c[i].Residence_Type__c;
Borrower.Organisation__c = c[i].Organisation__c;
date datetoday = system.today();
Integer todayyr = datetoday.year();
Integer birthyr = c[i].Birthdate.year();
Age = (Todayyr - birthyr);
system.debug('Age :' + Age);
dateInStringString = string.valueOfGmt(c[i].Birthdate);
system.debug('dateInStringString :' +dateInStringString);
MM = dateInStringString.substring(5, 7);
system.debug('MM :' +MM);
DD = dateInStringString.substring(8, 10);
system.debug('DD :' +DD);
YYYY = dateInStringString.substring(0, 4);
system.debug('YYYY :' +YYYY);
// insert Borrower;
// bankfacility = [Select Bank_Account_Number__c from Bank_Facilities__c where Id = :c[i].id limit 1];
}
else if (c[i].Borrower_Type__c == 'Co-Borrower'){
CoBorrower.FirstName = c[i].FirstName;
CoBorrower.LastName = c[i].LastName;
CoBorrower.Birthdate = c[i].Birthdate;
CoBorrower.Gender__c = c[i].Gender__c;
CoBorrower.PAN_ID__c = c[i].PAN_ID__c;
CoBorrower.phone = c[i].phone;
CoBorrower.Annual_Income_Gross__c =c[i].Annual_Income_Gross__c;
CoBorrower.Marital_Status__c =c[i].Marital_Status__c;
CoBorrower.Email =c[i].Email;
CoBorrower.MobilePhone = c[i].MobilePhone;
CoBorrower.MailingStreet = c[i].MailingStreet;
CoBorrower.MailingCity = c[i].MailingCity;
CoBorrower.MailingState = c[i].MailingState;
CoBorrower.MailingCountry = c[i].MailingCountry;
CoBorrower.MailingPostalCode = c[i].MailingPostalCode;
CoBorrower.Residence_Type__c = c[i].Residence_Type__c;
CoBorrower.Organisation__c = c[i].Organisation__c;
//insert CoBorrower;
}
else if (c[i].Borrower_Type__c == 'Guarantor'){
Guarantor.FirstName =c[i].FirstName;
Guarantor.LastName = c[i].LastName;
Guarantor.Birthdate =c[i].Birthdate;
Guarantor.Gender__c = c[i].Gender__c;
Guarantor.PAN_ID__c = c[i].PAN_ID__c;
Guarantor.Phone =c[i].Phone;
Guarantor.Annual_Income_Gross__c=c[i].Annual_Income_Gross__c;
Guarantor.Marital_Status__c =c[i].Marital_Status__c;
Guarantor.Email = c[i].Email;
Guarantor.MobilePhone = c[i].MobilePhone;
Guarantor.MailingStreet = c[i].MailingStreet;
Guarantor.MailingCity = c[i].MailingCity;
Guarantor.MailingState = c[i].MailingState;
Guarantor.MailingCountry = c[i].MailingCountry;
Guarantor.MailingPostalCode = c[i].MailingPostalCode;
Guarantor.Residence_Type__c = c[i].Residence_Type__c;
Guarantor.Organisation__c = c[i].Organisation__c;
// insert Guarantor;
}
else if(c[i].Borrower_Type__c =='Patient'){
Patient.FirstName =c[i].FirstName;
Patient.LastName =c[i].LastName;
Patient.Birthdate =c[i].Birthdate;
Patient.Gender__c = c[i].Gender__c;
Patient.PAN_ID__c =c[i].PAN_ID__c;
Patient.Phone =c[i].Phone;
Patient.Annual_Income_Gross__c =c[i].Annual_Income_Gross__c;
Patient.Marital_Status__c =c[i].Marital_Status__c;
Patient.Email = c[i].Email;
Patient.MobilePhone =c[i].MobilePhone;
Patient.MailingStreet = c[i].MailingStreet;
Patient.MailingCity = c[i].MailingCity;
Patient.MailingState = c[i].MailingState;
Patient.MailingCountry = c[i].MailingCountry;
Patient.MailingPostalCode = c[i].MailingPostalCode;
Patient.Residence_Type__c = c[i].Residence_Type__c;
Patient.Organisation__c = c[i].Organisation__c;
// insert Patient;
}
}
string controlXml ='Some String';
string batchXml = 'Some String';
string username = 'Username';
string password = 'password';
MatchService.MatchingSoap c1 = new MatchService.MatchingSoap(); // instantiate Class
string response_x = c1.Match(controlXml, batchXml, username, password);
DOM.Document xmlDOC = new DOM.Document();
xmlDOC.load(response_x);
DOM.XMLNode rootElement = xmlDOC.getRootElement();
// System.Debug('Root Element :: '+rootelement);
for(DOM.XMLNode xmlNodeObj : xmlDOC.getRootElement().getChildElements()){
// System.Debug('Child Elements :: '+xmlNodeObj);
for(DOM.XMLNode xmlNodeObjChild:xmlNodeObj.getChildren())
{
// System.Debug('$$$ Childrens'+xmlNodeObjChild.getChildren());
if(xmlNodeObjChild.getName()=='MatchSchemes')
System.Debug('-----------' + xmlNodeObjChild.getText());
if(xmlNodeObjChild.getName()=='Scheme')
System.Debug('-----------' + xmlNodeObjChild.getText());
if(xmlNodeObjChild.getName()=='SchemeID')
System.Debug('-----------' + xmlNodeObjChild.getText());
if(xmlNodeObjChild.getName()=='Score')
System.Debug('-----------' + xmlNodeObjChild.getText());
}
if (xmlNodeObj.getAttributeCount() > 0) {
for (Integer j = 0; j< xmlNodeObj.getAttributeCount(); j++ ) {
result += xmlNodeObj.getAttributeValue(xmlNodeObj.getAttributeKeyAt(j), xmlNodeObj.getAttributeKeyNsAt(j));
system.debug('Result : '+ result);
}
}
}
}
}
And MatchService(Class@3) class which is generated from WSDL
Test Class:
@isTest
Public class A_Test{
Public static testMethod void testcase(){
Account acc = new Account();
acc.Name = 'AccountTest';
acc.Sector__c = 'Education';
insert acc;
Opportunity opp = new Opportunity();
opp.Name = 'Test';
opp.StageName = 'Rejected';
opp.Collateral_Type__c = 'Other';
opp.Application_Creation_Date__c= date.newInstance(2016, 03, 11);
opp.AccountId = acc.Id;
insert opp;
String rid = opp.id;
String aid= acc.id;
system.debug('Aid --'+aid);
Contact con = new Contact();
con.LastName = 'Ram';
con.AccountId = acc.Id;
insert con;
String cid = con.id;
system.debug('ConId---'+cid);
system.debug('-----'+rid);
ApexPages.CurrentPage().getParameters().put('id',rid);
ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(opp);
A hc = new A(sc);
hc.method();
}
}
Now am getting the error as Methods defined as TestMethod do not support Web service callouts. Let me know wat to do...
Usually if(!Test.isRunningTest()) can be used in your class where you are invoking the stub generated from the WSDL, but it entier stub code will not be executed during test class run.
So you can add this condition in the stub for the method WebServiceCallout.invoke(.....)
I have used both approaches and it worked for me.
Can you provide some code on this..
Thanks..
If i dont use the if(!Test.isRunningTest()) in the class then i am getting the error as Methods defined as TestMethod do not support Web service callouts.
What should i do now.???
Is there any way to get the dummy response in test class??
Also make sure you include just the invoke method inside IF block and no other piece of code.
if(!Test.isRunningTest()){
WebServiceCallout.invoke(
this,
request_x,
response_map_x,
new String[]{endpoint_x,
'http://www.test.com',
'http://www.test.com',
'Match',
'http://www.test.com',
'MatchResponse',
'MatchService.MatchResponse_element'}
);
}
I am getting the Null error at Line response_x = response_map_x.get('response_x'); in class which is generated from WSDL.
Please help on this. I am stuck at test class error..
Thanks in advance..
Modify your class as shown here.
if(!Test.isRunningTest()){
WebServiceCallout.invoke(........);
response_x = response_map_x.get('response_x');
system.debug('response_x:' + response_x);
return response_x.MatchResult;
}else{
return null;
}
I did the same but the code coverage is decreasing if i put the IF condition in the class.Now the code coverage is 57%.
Without 75% code coverage i can not move into production.
Overall code coerage for all the classes should be more than 75% in the org and it is not requried for each class to be more than 75%. Ofcourse it is a best practice to follow to keep each class code coverage more than 75%, but here, for your stub, it is difficult to achive 75% code coverage.
The otherway around, if your compnay/client insisting on each class code coverage should be more than 75%, is to add dummy methods and call them from the test class, but this is not adviasable.