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
srinu namani 12srinu namani 12 

Im much comforatable in writing test classes for normal classes but not for the one's like below. can anybody help me out here please...

global  class TerritoryAssignmentWebService {
    public static boolean isRecursive = false;

    @future(callout=true)
    global static void invokeTerritoryAssignmentRules(Set<Id> accountIds, String sessionId, Map<Id,String> accountAssignedTerrMap){
        isRecursive = true;
        try{
        system.debug('accountAssignedTerrMap Print>>>'+accountAssignedTerrMap);
        system.debug('accountIds Print>>>'+accountIds);
        HttpRequest req = new HttpRequest(); 
        //Set HTTPRequest Method
        req.setMethod('POST');
        //Set HTTPRequest header properties
        req.setHeader('content-type', 'text/xml');
        req.setHeader('SOAPAction','""');
        req.setEndpoint(Label.SFDC_Webservice_Endpoint);
        String sobjectUpdateString = '';
        FieldSalesProperties__c fsprops =  FieldSalesProperties__c.getValues('IsRecursive');
        if(fsprops!=null && fsprops.value__c=='False'){
          if(accountIds!=null){
              for(Id accId : accountIds){
                   sobjectUpdateString +=  '<urn:sObjects  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Account">'
                                              + '<urn1:Id>' + accId + '</urn1:Id>'
                                           + '</urn:sObjects>';
                                       
              }
          }else if(accountAssignedTerrMap!=null){
           system.debug('>>Inside else if'+accountAssignedTerrMap);
              for(Id accId: accountAssignedTerrMap.keyset()){
                  sobjectUpdateString +=  '<urn:sObjects  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Account">'
                                              + '<urn1:Id>' + accId + '</urn1:Id>'
                                              + '<urn1:Assigned_Territories__c>' + accountAssignedTerrMap.get(accId)+ '</urn1:Assigned_Territories__c>'
                                              + '</urn:sObjects>';
              }
              system.debug('fsprops ...'+fsprops);
              sobjectUpdateString +=  '<urn:sObjects  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="FieldSalesProperties__c">'
                                              + '<urn1:Id>' + fsprops.Id+ '</urn1:Id>'
                                              + '<urn1:Value__c>True</urn1:Value__c>'
                                              + '</urn:sObjects>';
          
          }
          
        }
        
        //Preapre SOAP envelope
        String body = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:enterprise.soap.sforce.com" '
                                + 'xmlns:urn1="urn:sobject.enterprise.soap.sforce.com">'
                         + '<soapenv:Header>'
                             + '<urn:AssignmentRuleHeader>'
                                + '<urn:useDefaultRule>true</urn:useDefaultRule>'
                              + '</urn:AssignmentRuleHeader>'
                              + '<urn:SessionHeader>'
                                 + '<urn:sessionId>' + sessionId + '</urn:sessionId>'
                              + '</urn:SessionHeader>'
                         + '</soapenv:Header>'
                         + '<soapenv:Body>'
                              + '<urn:update>'
                                 + sobjectUpdateString
                               + '</urn:update>'
                         + '</soapenv:Body>'
                      + '</soapenv:Envelope>';
    
        //Set the HTTPRequest body
        system.debug('>>>> Value'+fsprops.value__c);
        if(fsprops !=null && fsprops.value__c=='False'){
        req.setBody(body);
        Http http = new Http();
        HTTPResponse res;
        //Execute web service call here 
        system.debug('>>>>SobjString'+body );
        System.debug('userinfo:'+Userinfo.getUserId());
        System.debug('sessionId:'+UserInfo.getSessionId()); 
        if (!Test.isRunningTest())
            res = http.send(req);
        }else{
            fsprops.value__c = 'False';
            update     fsprops;
        }
       /** System.debug(res.toString());
        System.debug('STATUS:'+body);
        System.debug('STATUS:'+res.getStatus());
        System.debug('STATUS_CODE:'+res.getStatusCode());
        System.debug('STATUS_CODE:'+res.getBody());*/
        }catch(Exception e){
            ApplicationDebugLog.captureExceptionDetails(e);
        }
    } 
    
}
James LoghryJames Loghry
For calling a web service directly, you'll want to use the HttpCalloutMock interface and construct the response you'll be expecting in your unit test.  Conversely, if you're using an Apex2WSDL generated class, then you'll want to use the WebserviceMock interface.  

Be sure to test all the expected conditions too.  For instance, you'll want to create responses for the positive cases were records are returned, but negative and exception cases as well.

For more info see the following links: