You need to sign in to do that
Don't have an account?
Sandesh Vishwakarma 9
Hello Guys , I have written a batch class that includes POST callout, can you please help me writing test class for this
public class BatchClassOrder implements Database.Batchable<ID>,Database.AllowsCallouts{
private List<ID> recordsToSend;
public BatchClassOrder(List<ID> recordsToSend){
this.recordsToSend = recordsToSend;
}
public Iterable<ID> start(Database.BatchableContext BC){
return recordsToSend;
}
public void execute(Database.BatchableContext BC, List<ID> records){
String errormsg;
String body;
String responsee;
string status;
String Method = 'POST';
for(ID rec : records) {
List<Order> recc = [select id , name from Order where id = :rec];
BatchClassOrder.cPO_main newObjj = new BatchClassOrder.cPO_main(recc);
body = JSON.serialize(newObjj);
body= '[' + body+ ']';
System.debug('body' + body);
String endpoint;
System.debug('body' + body);
try {
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
endpoint = 'https://xxxxx-xxx.xxx.xx.xx/xxx/xx';
req.setHeader('client_id','xxxxxxxxxxxxxxxxx');
req.setHeader('client_secret','xxxxxxxxxxxxxxxxx');
req.setHeader('Content-Type', 'application/json');
req.setEndpoint(endpoint);
req.setMethod('POST');
req.setBody(body);
if (!Test.isRunningTest()) {
res = http.send(req);
responsee = res.getBody();
System.debug('getStatusCode' + res.getStatusCode());
if(res.getStatusCode() == 200){
status = 'Success';
}
else{
status = 'Error';
}
System.debug('Str:' + res.getStatusCode());
}
}
catch (Exception e) {
System.debug('Error:' + e.getMessage() + 'LN:' + e.getLineNumber() );
errormsg = e.getMessage();
}
}
String className = 'BatchClassOrder';
String API_Name = 'APINAME';
String Description = 'Description';
String Error_code_c = errormsg;
String Request_Type_c = 'Outbound';
String Request_c = body ;
String Response_c = responsee;
String Status_c = status;
datetime Start_time_c;
datetime End_time_c;
Decimal duration;
datetime Createddate;
LoggingDetails loggingdetailsObj = new LoggingDetails(); //It is new class that has a method that saves this information into a object
loggingdetailsObj.mainMethod(className,API_Name,Description,Error_code_c,Request_Type_c,Request_c,Response_c,Status_c,Start_time_c,End_time_c,duration,Createddate);
}
public void finish(Database.BatchableContext BC){
}
// Constructor that sets the values into a request body JSON
public class cPO_main {
public String name;
public cPo_main(List<order> recList){
for(Order o : recList){
name = o.Name;
}
}
}
private List<ID> recordsToSend;
public BatchClassOrder(List<ID> recordsToSend){
this.recordsToSend = recordsToSend;
}
public Iterable<ID> start(Database.BatchableContext BC){
return recordsToSend;
}
public void execute(Database.BatchableContext BC, List<ID> records){
String errormsg;
String body;
String responsee;
string status;
String Method = 'POST';
for(ID rec : records) {
List<Order> recc = [select id , name from Order where id = :rec];
BatchClassOrder.cPO_main newObjj = new BatchClassOrder.cPO_main(recc);
body = JSON.serialize(newObjj);
body= '[' + body+ ']';
System.debug('body' + body);
String endpoint;
System.debug('body' + body);
try {
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
endpoint = 'https://xxxxx-xxx.xxx.xx.xx/xxx/xx';
req.setHeader('client_id','xxxxxxxxxxxxxxxxx');
req.setHeader('client_secret','xxxxxxxxxxxxxxxxx');
req.setHeader('Content-Type', 'application/json');
req.setEndpoint(endpoint);
req.setMethod('POST');
req.setBody(body);
if (!Test.isRunningTest()) {
res = http.send(req);
responsee = res.getBody();
System.debug('getStatusCode' + res.getStatusCode());
if(res.getStatusCode() == 200){
status = 'Success';
}
else{
status = 'Error';
}
System.debug('Str:' + res.getStatusCode());
}
}
catch (Exception e) {
System.debug('Error:' + e.getMessage() + 'LN:' + e.getLineNumber() );
errormsg = e.getMessage();
}
}
String className = 'BatchClassOrder';
String API_Name = 'APINAME';
String Description = 'Description';
String Error_code_c = errormsg;
String Request_Type_c = 'Outbound';
String Request_c = body ;
String Response_c = responsee;
String Status_c = status;
datetime Start_time_c;
datetime End_time_c;
Decimal duration;
datetime Createddate;
LoggingDetails loggingdetailsObj = new LoggingDetails(); //It is new class that has a method that saves this information into a object
loggingdetailsObj.mainMethod(className,API_Name,Description,Error_code_c,Request_Type_c,Request_c,Response_c,Status_c,Start_time_c,End_time_c,duration,Createddate);
}
public void finish(Database.BatchableContext BC){
}
// Constructor that sets the values into a request body JSON
public class cPO_main {
public String name;
public cPo_main(List<order> recList){
for(Order o : recList){
name = o.Name;
}
}
}
Can you try the test class as below.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,