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 

How to write a test class when @Future annotation is used in the class.



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);
        }
    } 
    
}
Andries.NeyensAndries.Neyens
Just make sure you do your asserts after the Test.stopTest().
This command will trigger and finish all the future calls.
James LoghryJames Loghry
When you invoke your method, it should be invoked between calls to Test.startTest(); and Test.stopTest();  This will assure that any asynchronous transactions finish before the Test.stopTest(); finishes executing.

To recap you should do the following in your unit test:
  1. Set up any mock data
  2. Call Test.startTest();
  3. Invoke  your invokeTerritoryAssignmentRules method
  4. Call Test.stopTest();
  5. Use System.assert methods to assert your conditions and outcomes of the test are correct / as expected.