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
rock world 52rock world 52 

hello all how to write test class for following code..

public class ScheduleDashboardRefreshBatch implements Database.Batchable<sObject>,Database.AllowsCallouts, Database.Stateful {
    public string accesstoken;
    public Database.QueryLocator start(Database.BatchableContext bc) {
        string orgId = UserInfo.getOrganizationId();
        string suborgId=orgId.substring(0,15);
        string orgbody=Label.LoginRequest.replace('@@@@OrgId@@@@',suborgId);
        string userbody=orgbody.replace('@@@@Username@@@@',Label.Username);
        string Pwdbody=userbody.replace('@@@@Password@@@@',Label.Password);
        string body = Pwdbody;
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://login.salesforce.com/services/Soap/u/29.0');
        req.setMethod('POST');
        req.setHeader('Content-Type', 'text/xml;charset=UTF-8');
        req.setHeader('SOAPAction', '""');
        req.setBody(body);
        Http p = new Http();
        HttpResponse res = p.send(req);
        Xmlstreamreader reader = new Xmlstreamreader(res.getbody());
        while (reader.hasNext()) {
            if (reader.getEventType() == XmlTag.Characters) {
                if (string.valueof(reader.getText()).startswith(suborgId + '!')) {
                    accesstoken = reader.getText();
                }
            }
            reader.next();
        } 
        String queryString = 'Select Id from Dashboard';
        return Database.getQueryLocator(queryString);
    }
    public void execute(Database.BatchableContext bc, List&lt;sObject&gt;  scope) {
        try {
            for(Sobject s : scope){
                Dashboard dr = (Dashboard)s;
                String requestUrl = '/dash/dashboardRefresh.apexp?id='+dr.Id;
                Http http = new Http();
                HttpRequest req = new HttpRequest();
                req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + requestUrl);
                req.setMethod('GET');
                req.setHeader('Cookie','sid='+accesstoken+'');
                HTTPResponse res = http.send(req);
                String output = res.getBody();
                System.debug('>>>>'+output);
            }
        } catch(Exception ex){
            system.debug('>>>>Refresh failed Due to :'+ ex.getmessage());
        }
         
    }
     public void finish(Database.BatchableContext bc) {
    }
}
Rahul KumarRahul Kumar (Salesforce Developers) 

Hope it will be helpful.

Please mark it as best answer if the information is informative.

Thanks
Rahul Kumar