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
Sandesh Vishwakarma 9Sandesh 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;
}
}
}
   
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the test class as below.
@istest
public class BatchClassOrderTest {
@isTest static void testtaskactivity() {
    Account a = new Account();
    a.Name = 'Test Account';
    insert a;

    // Insert Product
    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
    

    Id pricebookId = Test.getStandardPricebookId();
    
    // Insert PricebookEntry

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = pricebookId;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.IsActive = true;
    standardPrice.UseStandardPrice = false;
    insert standardPrice ;
    
    // Insert Order
    
    Order o = new Order();
    o.Name = 'Test Order ';
    o.Status = 'Draft';
    o.EffectiveDate = system.today();
    o.EndDate = system.today() + 4;
    o.AccountId = a.id;
    o.Pricebook2Id =  pricebookId ;
    
    insert o;
    List<Id>ordersids= new List<Id>();
    ordersids.add(o.id);
    BatchClassOrder shn = new BatchClassOrder(ordersids); 
        database.executeBatch(shn );
}
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,