You need to sign in to do that
Don't have an account?
error- Methods defined as TestMethod do not support Web service callouts
Hi friends.
I am getting this error for this batch class:- Methods defined as TestMethod do not support Web service callouts
Class is as below:
********************************
global class SalesAIChildDataSyncBatch implements Database.Batchable<sObject>, Database.AllowsCallouts, Schedulable{
global class Item {
global String status;
global Integer code;
global String message;
global Integer mappingId;
global String sessionToken;
global String userType;
global String name;
global String emailId;
global Data data;
global Item(){}
}
global class Data {
global Integer dataId;
global String oppId;
public String accountNumber;
public String accountId;
public String accountName;
public String contactName;
public String designation;
public String status;
public String type;
public String email;
public Integer contactYTDSales;
public Integer contactLeadScore;
public Integer leadScore;
public Integer opportunityScore;
public String nextBestAction;
public String productServiceRecommendation;
public String feedback;
public Integer thisWeek;
public Integer lastWeek;
public String primarySKU;
public String skuLabel;
public String CurrencyC;
public String businessUnit;
public String mobileNumber;
public String officeNumber;
public String productServiceRationale1;
public Integer productServiceScore1;
public String productServiceRationale2;
public Integer productServiceScore2;
public String productServiceRationale3;
public String productServiceRationale4;
public Integer productServiceScore4;
public String productServiceRationale5;
public Integer productServiceScore5;
public String productServiceRationale6;
public Integer productServiceScore6;
public String industryType;
public Date customerSince;
public String productCheatSheet1;
public String productCheatSheet2;
public String productCheatSheet3;
public String productCheatSheet4;
public String productCheatSheet5;
public String productCheatSheet6;
public String relatedProduct1;
public String relatedProduct2;
public String relatedProduct3;
public String relatedProduct4;
public String relatedProduct5;
public String relatedProduct6;
public String discountGuidance;
public String location;
public Data(){}
}
global Database.QueryLocator start(Database.BatchableContext BC){
Set<Id> accIds = new Set<Id>{'001N000001AJNSi'};
return Database.getQueryLocator('SELECT Id, AccountNumber, dataId__c, (SELECT Name FROM Contacts ORDER BY CreatedDate DESC LIMIT 1) FROM Account WHERE Id IN : accIds');
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
for(Account acc : (List<Account>) scope){
String contactName = '';
if(acc.Contacts != null && acc.Contacts.size() > 0){
contactName = acc.Contacts[0].Name;
}
String dataId = string.valueof(acc.dataId__c);
fetchData(acc.Id, dataId);
}
}
global void fetchData(Id accId, String dataidAcc){
navikAuthDomain.response mapResp = navikAuthentication.getMapId(UserInfo.getUserEmail());
Recommendation__c recomnd;
Recommendation_Rationale__c recomRational;
Cheatsheet__c cheatsht;
if(mapResp !=null){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setHeader('sessionToken',mapResp.sessionToken);
request.setEndpoint('http://35.163.113.123:8080/sales-core/v1.0/sales/recommendation/product_services?');
request.setMethod('POST');
//accId = EncodingUtil.urlEncode(accId,'UTF-8');
dataidAcc = EncodingUtil.urlEncode(dataidAcc,'UTF-8');
//cont = EncodingUtil.urlEncode(cont,'UTF-8');
String recom = EncodingUtil.urlEncode('true','UTF-8');
request.setBody('dataId='+dataidAcc);
HttpResponse response = http.send(request);
/* if(!test.isRunningTest()){
response = http.send(request);}
else {
response=new HttpResponse();
} */
System.debug('>>>>' + response.getBody());
if (response.getStatusCode() == 200) {
Item lstData = (Item)JSON.deserialize(response.getBody(), Item.class);
//SalesAIRecommendations.Item item = (SalesAIRecommendations.Item)JSON.deserialize(response.getBody(), SalesAIRecommendations.Item.class);
System.debug('>>>>lstData'+lstData);
if(lstData != null && lstData.data != null ){
//Map<Id, Recommendation__c> accRecommMap = new Map<Id, Recommendation__c>();
//Map<Id, Recommendation_Rationale__c> RecomRatnlMap = new Map<Id, Recommendation_Rationale__c>();
//Map<Id, Cheatsheet__c> cheatShtMap = new Map<Id, Cheatsheet__c>();
Data freshData = lstData.Data;
System.debug('Accid>>>>' + accId);
recomnd = new Recommendation__c(Opportunity_Score__c = freshData.opportunityScore,
Product_Service__c = freshData.productServiceRecommendation,
Est_Value__c = freshData.contactYTDSales,
Account__c = accId );
// accRecommMap.put(data.accountNumber, recomnd);
//Recommendation Rationale Upsert
recomRational = new Recommendation_Rationale__c();
recomRational.Rationale1__c = freshData.productServiceRationale1;
recomRational.Rationale2__c = freshData.productServiceRationale2;
recomRational.Rationale3__c = freshData.productServiceRationale3;
recomRational.Rationale4__c = freshData.productServiceRationale4;
recomRational.Rationale5__c = freshData.productServiceRationale5;
recomRational.Rationale6__c = freshData.productServiceRationale6;
//RecomRatnlMap.put(data.accountNumber, recomRational);
//Cheatsheet upsert
cheatsht = new Cheatsheet__c();
cheatsht.Discount_Guidance__c = freshData.discountGuidance;
cheatsht.Pitch1__c = freshData.productCheatSheet1;
cheatsht.Pitch2__c = freshData.productCheatSheet2;
cheatsht.Pitch3__c = freshData.productCheatSheet3;
cheatsht.Pitch4__c = freshData.productCheatSheet4;
cheatsht.Pitch5__c = freshData.productCheatSheet5;
cheatsht.Pitch6__c = freshData.productCheatSheet6;
cheatsht.RelatedProducts1__c = freshData.relatedProduct1;
cheatsht.RelatedProducts2__c = freshData.relatedProduct2;
cheatsht.RelatedProducts3__c = freshData.relatedProduct3;
cheatsht.RelatedProducts4__c = freshData.relatedProduct4;
cheatsht.RelatedProducts5__c = freshData.relatedProduct5;
cheatsht.RelatedProducts6__c = freshData.relatedProduct6;
//cheatShtMap.put(data.accountNumber, cheatsht);
}
upsert recomnd;
system.debug('recomnd >> '+recomnd.Id);
recomRational.Recommendation__c = recomnd.Id;
upsert recomRational;
cheatsht.Recommendation__c= recomnd.Id;
upsert cheatsht;
}
}
}
global void finish(Database.BatchableContext BC){}
global void execute(SchedulableContext sc){
database.executebatch(new SalesAIChildDataSyncBatch());
}
}
I am getting this error for this batch class:- Methods defined as TestMethod do not support Web service callouts
Class is as below:
********************************
global class SalesAIChildDataSyncBatch implements Database.Batchable<sObject>, Database.AllowsCallouts, Schedulable{
global class Item {
global String status;
global Integer code;
global String message;
global Integer mappingId;
global String sessionToken;
global String userType;
global String name;
global String emailId;
global Data data;
global Item(){}
}
global class Data {
global Integer dataId;
global String oppId;
public String accountNumber;
public String accountId;
public String accountName;
public String contactName;
public String designation;
public String status;
public String type;
public String email;
public Integer contactYTDSales;
public Integer contactLeadScore;
public Integer leadScore;
public Integer opportunityScore;
public String nextBestAction;
public String productServiceRecommendation;
public String feedback;
public Integer thisWeek;
public Integer lastWeek;
public String primarySKU;
public String skuLabel;
public String CurrencyC;
public String businessUnit;
public String mobileNumber;
public String officeNumber;
public String productServiceRationale1;
public Integer productServiceScore1;
public String productServiceRationale2;
public Integer productServiceScore2;
public String productServiceRationale3;
public String productServiceRationale4;
public Integer productServiceScore4;
public String productServiceRationale5;
public Integer productServiceScore5;
public String productServiceRationale6;
public Integer productServiceScore6;
public String industryType;
public Date customerSince;
public String productCheatSheet1;
public String productCheatSheet2;
public String productCheatSheet3;
public String productCheatSheet4;
public String productCheatSheet5;
public String productCheatSheet6;
public String relatedProduct1;
public String relatedProduct2;
public String relatedProduct3;
public String relatedProduct4;
public String relatedProduct5;
public String relatedProduct6;
public String discountGuidance;
public String location;
public Data(){}
}
global Database.QueryLocator start(Database.BatchableContext BC){
Set<Id> accIds = new Set<Id>{'001N000001AJNSi'};
return Database.getQueryLocator('SELECT Id, AccountNumber, dataId__c, (SELECT Name FROM Contacts ORDER BY CreatedDate DESC LIMIT 1) FROM Account WHERE Id IN : accIds');
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
for(Account acc : (List<Account>) scope){
String contactName = '';
if(acc.Contacts != null && acc.Contacts.size() > 0){
contactName = acc.Contacts[0].Name;
}
String dataId = string.valueof(acc.dataId__c);
fetchData(acc.Id, dataId);
}
}
global void fetchData(Id accId, String dataidAcc){
navikAuthDomain.response mapResp = navikAuthentication.getMapId(UserInfo.getUserEmail());
Recommendation__c recomnd;
Recommendation_Rationale__c recomRational;
Cheatsheet__c cheatsht;
if(mapResp !=null){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setHeader('sessionToken',mapResp.sessionToken);
request.setEndpoint('http://35.163.113.123:8080/sales-core/v1.0/sales/recommendation/product_services?');
request.setMethod('POST');
//accId = EncodingUtil.urlEncode(accId,'UTF-8');
dataidAcc = EncodingUtil.urlEncode(dataidAcc,'UTF-8');
//cont = EncodingUtil.urlEncode(cont,'UTF-8');
String recom = EncodingUtil.urlEncode('true','UTF-8');
request.setBody('dataId='+dataidAcc);
HttpResponse response = http.send(request);
/* if(!test.isRunningTest()){
response = http.send(request);}
else {
response=new HttpResponse();
} */
System.debug('>>>>' + response.getBody());
if (response.getStatusCode() == 200) {
Item lstData = (Item)JSON.deserialize(response.getBody(), Item.class);
//SalesAIRecommendations.Item item = (SalesAIRecommendations.Item)JSON.deserialize(response.getBody(), SalesAIRecommendations.Item.class);
System.debug('>>>>lstData'+lstData);
if(lstData != null && lstData.data != null ){
//Map<Id, Recommendation__c> accRecommMap = new Map<Id, Recommendation__c>();
//Map<Id, Recommendation_Rationale__c> RecomRatnlMap = new Map<Id, Recommendation_Rationale__c>();
//Map<Id, Cheatsheet__c> cheatShtMap = new Map<Id, Cheatsheet__c>();
Data freshData = lstData.Data;
System.debug('Accid>>>>' + accId);
recomnd = new Recommendation__c(Opportunity_Score__c = freshData.opportunityScore,
Product_Service__c = freshData.productServiceRecommendation,
Est_Value__c = freshData.contactYTDSales,
Account__c = accId );
// accRecommMap.put(data.accountNumber, recomnd);
//Recommendation Rationale Upsert
recomRational = new Recommendation_Rationale__c();
recomRational.Rationale1__c = freshData.productServiceRationale1;
recomRational.Rationale2__c = freshData.productServiceRationale2;
recomRational.Rationale3__c = freshData.productServiceRationale3;
recomRational.Rationale4__c = freshData.productServiceRationale4;
recomRational.Rationale5__c = freshData.productServiceRationale5;
recomRational.Rationale6__c = freshData.productServiceRationale6;
//RecomRatnlMap.put(data.accountNumber, recomRational);
//Cheatsheet upsert
cheatsht = new Cheatsheet__c();
cheatsht.Discount_Guidance__c = freshData.discountGuidance;
cheatsht.Pitch1__c = freshData.productCheatSheet1;
cheatsht.Pitch2__c = freshData.productCheatSheet2;
cheatsht.Pitch3__c = freshData.productCheatSheet3;
cheatsht.Pitch4__c = freshData.productCheatSheet4;
cheatsht.Pitch5__c = freshData.productCheatSheet5;
cheatsht.Pitch6__c = freshData.productCheatSheet6;
cheatsht.RelatedProducts1__c = freshData.relatedProduct1;
cheatsht.RelatedProducts2__c = freshData.relatedProduct2;
cheatsht.RelatedProducts3__c = freshData.relatedProduct3;
cheatsht.RelatedProducts4__c = freshData.relatedProduct4;
cheatsht.RelatedProducts5__c = freshData.relatedProduct5;
cheatsht.RelatedProducts6__c = freshData.relatedProduct6;
//cheatShtMap.put(data.accountNumber, cheatsht);
}
upsert recomnd;
system.debug('recomnd >> '+recomnd.Id);
recomRational.Recommendation__c = recomnd.Id;
upsert recomRational;
cheatsht.Recommendation__c= recomnd.Id;
upsert cheatsht;
}
}
}
global void finish(Database.BatchableContext BC){}
global void execute(SchedulableContext sc){
database.executebatch(new SalesAIChildDataSyncBatch());
}
}
********************
@isTest(SeeAllData=true)
public class SalesAIChildDataSyncBatchTest {
public static testmethod void childbatch(){
Set<Id> accIds = new Set<Id>{'001N000001AJNSi'};
List<Account> lst = new List<Account>();
List<Contact> conlist = new List<Contact>();
Account acc = new Account();
acc.name = 'Jon';
lst.add(acc);
Contact con = new Contact();
con.Lastname = 'Raj';
con.AccountId = acc.Id;
conlist.add(con);
/*
Recommendation__c recomnd = new Recommendation__c();
recomnd.Opportunity_Score__c = 90;
recomnd.Product_Service__c = 'good';
recomnd.Est_Value__c = 500;
recomnd.Account__c = acc.id;
insert recomnd;
*/
//Set<Id> accIds = new Set<Id>{'001N000001AJNSi'};
/*
List<Recommendation__c> rc = new List<Recommendation__c>();
Recommendation__c recomnd = new Recommendation__c(Opportunity_Score__c = 90,
Product_Service__c = 'good',
Est_Value__c = 500);
insert recomnd;
system.debug('#########################'+ recomnd); */
//List<Recommendation_Rationale__c> reclist = new List<Recommendation_Rationale__c>();
/* Recommendation_Rationale__c rec = new Recommendation_Rationale__c();
rec.Name = 'test';
rec.Rationale1__c = 'test1';
rec.Rationale2__c = 'test2';
rec.Rationale3__c = 'test3';
rec.Rationale4__c = 'test4';
rec.Rationale5__c = 'test5';
rec.Rationale6__c = 'test6';
rec.Recommendation__c = recomnd.Id; */
/*
rec.Recommendation__c = ApexPages.currentPage().getParameters().get('id');
/*rec.Recommendation__cId = rc.Id;
rc.add(rec); */
//upsert rec;
/*
Cheatsheet__c cht= new Cheatsheet__c();
cht.Discount_Guidance__c = 'help';
cht.Pitch1__c = 'Pitch1';
cht.Pitch2__c = 'Pitch2';
cht.Pitch3__c = 'Pitch3';
cht.Pitch4__c = 'Pitch4';
cht.Pitch5__c = 'Pitch5';
cht.Pitch6__c = 'Pitch6';
cht.RelatedProducts1__c = 'p1';
cht.RelatedProducts2__c = 'p2';
cht.RelatedProducts3__c = 'p3';
cht.RelatedProducts4__c = 'p4';
cht.RelatedProducts5__c = 'p5';
cht.RelatedProducts6__c = 'p6';
// cht.Recommendation__c= recomnd.Id;
cht.Recommendation__c= ApexPages.currentPage().getParameters().get('id');
upsert cht;
*/
Test.startTest();
SalesAIChildDataSyncBatch child = new SalesAIChildDataSyncBatch();
DataBase.executeBatch(child, 1);
test.stopTest();
Test.setMock(HttpCalloutMock.class, new SalesAIChildDataSyncBatchMockGenerator());
HttpResponse res = SalesAIChildDataSyncBatch.fetchData(acc.Id, '');
String contentType = res.getHeader('Content-Type');
System.assert(contentType == 'application/json');
String actualValue = res.getBody();
String expectedValue = '{"foo":"bar"}';
System.assertEquals(actualValue, expectedValue);
System.assertEquals(200, res.getStatusCode());
}
}
Please go through the below link which shows how to test callouts.
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex_testing.htm (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex_testing.htm )
https://salesforce.stackexchange.com/questions/3486/testing-httpcallout-with-httpcalloutmock-and-unittest-created-data
Thanks
Lokesh
Please check once below code may useful to you.
For callouts we need to implement HttpCalloutMock interface then we need to call in test class.
Thanks
Varaprasad
Thanks you for responding. Still getting the same error. I also tried this code for passing default value:
****************************************
if(!test.isRunningTest())
mapResp = navikAuthentication.getMapId(UserInfo.getUserEmail());
else
{
mapResp = new navikAuthDomain.response();
mapResp.sessionToken='121122';
}
********************************************
I have also checked mock generator and its giving the same error and I a getting 41% coverage only.
Error- Methods defined as TestMethod do not support Web service callouts
*******************************
@isTest(SeeAllData=true)
public class SalesAIChildDataSyncBatchTest {
@istest public static void childbatch(){
Set<Id> accIds = new Set<Id>{'001N000001AJNSi'};
List<Account> lst = new List<Account>();
List<Contact> conlist = new List<Contact>();
Account acc = new Account();
acc.name = 'Jon';
lst.add(acc);
Contact con = new Contact();
con.Lastname = 'Raj';
con.AccountId = acc.Id;
conlist.add(con);
List<Recommendation__c> rc = new List<Recommendation__c>();
Recommendation__c recomnd = new Recommendation__c();
recomnd.Opportunity_Score__c = 90;
recomnd.Product_Service__c = 'good';
recomnd.Est_Value__c = 500;
recomnd.Account__c = acc.id;
rc.add(recomnd);
insert rc;
//Set<Id> accIds = new Set<Id>{'001N000001AJNSi'};
List<Recommendation_Rationale__c> reclist = new List<Recommendation_Rationale__c>();
Recommendation_Rationale__c rec = new Recommendation_Rationale__c();
rec.Name = 'test';
rec.Rationale1__c = 'test1';
rec.Rationale2__c = 'test2';
rec.Rationale3__c = 'test3';
rec.Rationale4__c = 'test4';
rec.Rationale5__c = 'test5';
rec.Rationale6__c = 'test6';
rec.Recommendation__c = recomnd.Id;
reclist.add(rec);
insert reclist;
List<Cheatsheet__c> cheatlist = new List<Cheatsheet__c>();
Cheatsheet__c cht= new Cheatsheet__c();
cht.Discount_Guidance__c = 'help';
cht.Pitch1__c = 'Pitch1';
cht.Pitch2__c = 'Pitch2';
cht.Pitch3__c = 'Pitch3';
cht.Pitch4__c = 'Pitch4';
cht.Pitch5__c = 'Pitch5';
cht.Pitch6__c = 'Pitch6';
cht.RelatedProducts1__c = 'p1';
cht.RelatedProducts2__c = 'p2';
cht.RelatedProducts3__c = 'p3';
cht.RelatedProducts4__c = 'p4';
cht.RelatedProducts5__c = 'p5';
cht.RelatedProducts6__c = 'p6';
cht.Recommendation__c= recomnd.Id;
cheatlist.add(cht);
upsert cheatlist;
Test.startTest();
SalesAIChildDataSyncBatch child = new SalesAIChildDataSyncBatch();
DataBase.executeBatch(child, 1);
test.stopTest();
}
please paste below code and chek once
Once it will work remove seeAlldata = true and create testdata.
Thanks
Varaprasad
Yes it is working but its giving the same coverage even I am inserting new records also and giving error like:
Error- System.NullPointerException: Attempted to upsert a null list
Can you please check my abouve code and suggest me how to upsert record so that it will accept.
Thank you in advance.
insert allrecords one by one lke below than pass record id like account.id.
insert lst.
insert conlist.
Thanks
Varaprasad
I have created records but its not affecting in coverage and showing the same coverage.
I think their is some problem with data arrangement. Can you plz check my above test in which I have already created record?. Can you plz arrange in a proper manner because I am new in testing and confused with all these because I have faced so many problems in testing and their is no one for guide me here. It will be great help for me and I'll also get my mistake.
Remove seeAlldata = true in production you have data then no need delete seeAllData .
Thanks
Varaprsad
System.NullPointerException: Attempted to upsert a null list
I'm getting the error - System.NullPointerException: Attempted to upsert a null list
Class.SalesAIChildDataSyncBatch.fetchData: line 185, column 1
Class.SalesAIChildDataSyncBatch.execute: line 89, column 1
Please check and help me because i am also inserting record in list but it is not taking value.
My complete test code is
**********************
@isTest(SeeAllData=true)
public class SalesAIChildDataSyncBatchTest {
public static testmethod void childbatch(){
List<Account> lstacc = new List<Account>();
Account acc = new Account(Account_Lead_Score__c = 99,
Industry = 'Education',
// DataId__c = data.dataId,
Name = 'Test',
ProductService_Recommendation__c = 'Test',
Feedback__c = 'Best',
thisweek__c = true,
lastweek__c = true,
primarySKU__c = '',
skuLabel__c = '',
BillingCity = 'Delhi',
Description = 'Education',
Customer_Since__c = system.today(),
AccountNumber = '8239723',
Last_Action__c = 'call',
Account_YTDSales__c = 1000);
lstacc.add(acc);
upsert lstacc;
List<Contact> lstcon = new List<Contact>();
Contact con = new Contact();
con.Lastname = 'Raj';
con.AccountId = acc.Id;
lstcon.add(con);
upsert lstcon;
List<Recommendation__c> lstrec = new List<Recommendation__c>();
Recommendation__c recomnd = new Recommendation__c();
recomnd.Opportunity_Score__c = 90;
recomnd.Product_Service__c = 'good';
recomnd.Est_Value__c = 500;
recomnd.Account__c = acc.id;
lstrec.add(recomnd);
upsert lstrec;
List<Recommendation_Rationale__c> lstrrc = new List<Recommendation_Rationale__c>();
Recommendation_Rationale__c rec = new Recommendation_Rationale__c();
rec.Name = 'test';
rec.Rationale1__c = 'test1';
rec.Rationale2__c = 'test2';
rec.Rationale3__c = 'test3';
rec.Rationale4__c = 'test4';
rec.Rationale5__c = 'test5';
rec.Rationale6__c = 'test6';
rec.Recommendation__c = recomnd.Id;
lstrrc.add(rec);
upsert lstrrc;
List<Cheatsheet__c> lstcht = new List<Cheatsheet__c>();
Cheatsheet__c cht= new Cheatsheet__c();
cht.Discount_Guidance__c = 'help';
cht.Pitch1__c = 'Pitch1';
cht.Pitch2__c = 'Pitch2';
cht.Pitch3__c = 'Pitch3';
cht.Pitch4__c = 'Pitch4';
cht.Pitch5__c = 'Pitch5';
cht.Pitch6__c = 'Pitch6';
cht.RelatedProducts1__c = 'p1';
cht.RelatedProducts2__c = 'p2';
cht.RelatedProducts3__c = 'p3';
cht.RelatedProducts4__c = 'p4';
cht.RelatedProducts5__c = 'p5';
cht.RelatedProducts6__c = 'p6';
cht.Recommendation__c= recomnd.Id;
lstcht.add(cht);
upsert lstcht;
Test.startTest();
Test.setMock(HttpCalloutMock.class, new SalesAIChildDataSyncBatchMockGenerator());
SalesAIChildDataSyncBatch child = new SalesAIChildDataSyncBatch();
DataBase.executeBatch(child, 1);
Test.stopTest();
}
}