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
MaheemSamMaheemSam 

How to cover @future method in test class code coverage

Hi, 

 I am writing test class for below class but @future method is not getting covered. Please suggest me how to cover. 
public class CpqQuoteTriggerUtil {

    public static void createQuoteShareFromOpportunity(List<SBQQ__Quote__c> newLst) {
        system.debug('Calling CpqQuoteTriggerUtil.createQuoteShareFromOpportunity' + newLst);
        Map<ID,ID> optyQuoteMap = new Map<ID,ID>();
        List<SBQQ__Quote__share> insertShares = new List<SBQQ__Quote__share>();
        List<SBQQ__Quote__share> currentUserShare = new List<SBQQ__Quote__share>();
        for(SBQQ__Quote__c quote : newLst) {
            if(quote.SBQQ__Opportunity2__c <> null) {
                optyQuoteMap.put(quote.SBQQ__Opportunity2__c, quote.id);
            }
            currentUserShare.add(new SBQQ__Quote__share(UserOrGroupId = userinfo.getUserId(),
                                                ParentId = quote.id,
                                                AccessLevel = 'Edit'));
        }
        //Map<Id, List<SBQQ__Quote__share>> oppToQuoteShareLst = new Map<Id, List<SBQQ__Quote__share>>();
        Boolean notOppOwnerFlg = false;
        Set<Id> userids = new Set<Id>();
        ID OptyOwnerId;
        Id qid;
        //for Opportunity share
        for(OpportunityShare r : [SELECT Id, OpportunityId,Opportunity.ownerid, UserOrGroupId, OpportunityAccessLevel, 
                                            RowCause 
                                            FROM OpportunityShare 
                                            where opportunityid = :optyQuoteMap.keyset() and RowCause in('Rule', 'Team')]) {
            if(userids.isEmpty()) {
                userids.add(r.Opportunity.ownerid);
                OptyOwnerId = r.Opportunity.ownerid;
                qid = optyQuoteMap.get(r.OpportunityId);
            }
            if(!userids.contains(r.UserOrGroupId) ) {
                userids.add(r.UserOrGroupId);
                //insertShares = new List<SBQQ__Quote__share>();
                insertShares.add(new SBQQ__Quote__share(UserOrGroupId = r.UserOrGroupId,
                                                    ParentId = optyQuoteMap.get(r.OpportunityId),
                                                    AccessLevel = r.OpportunityAccessLevel == 'All' ? 'Edit': r.OpportunityAccessLevel));
                if(r.Opportunity.ownerid != userinfo.getUserId()) {
                    notOppOwnerFlg = true;
                }
                system.debug(insertShares);
            }
        }

        //for Default Opportunity Teams // sudhir Test here for 
        for(UserTeamMember r : [SELECT Id,OpportunityAccessLevel,OwnerId,TeamMemberRole,UserId 
                                                FROM UserTeamMember where ownerid   = :OptyOwnerId] ) {
                insertShares.add(new SBQQ__Quote__share(UserOrGroupId = r.UserId,
                                                    ParentId = qid,
                                                    AccessLevel = r.OpportunityAccessLevel));
        }
        system.debug('share from opty :: ' + insertShares);
        if(notOppOwnerFlg) {
            system.debug('for user share creator :: ' + currentUserShare);
            insertShares.addAll(currentUserShare);
        }

        Database.SaveResult[] srList = database.insert(insertShares, false);
        for (Database.SaveResult sr : srList) {
            if (sr.isSuccess()) {
                // Operation was successful, so get the ID of the record that was processed
                System.debug('Successfully inserted account. Account ID: ' + sr.getId());
            }
            else {
                // Operation failed, so get all errors                
                for(Database.Error err : sr.getErrors()) {
                    System.debug('The following error has occurred.');                    
                    System.debug(err.getStatusCode() + ': ' + err.getMessage());
                    System.debug('Account fields that affected this error: ' + err.getFields());
                }
            }
        }

    }
    public static void prepopulateApproverLevelFromOwner(List<SBQQ__Quote__c> newLst) {
        Set<id> opps = new set<id>();
            for(SBQQ__Quote__c quote : newLst) {
            if(quote.SBQQ__Opportunity2__c <> null) {
                opps.add(quote.SBQQ__Opportunity2__c);
            }
        }
        
        Map<id, Opportunity> optyMap = new Map<id, Opportunity>([select id, owner.SPR_Approver_Director_Level__c,owner.SPR_Approver_EVP__c,
                                                owner.SPR_Approver_GVP__c,owner.SPR_Approver_RVP__c,owner.SPR_Approver_GD_Group_Director__c,
                                                 owner.SPR_Approver_RD_Regional_Director__c
                                                 from opportunity where id = :opps]);
        //Approval
        for(SBQQ__Quote__c quote : newLst) {
            if(quote.SBQQ__Opportunity2__c <> null) {
                Opportunity opp = optyMap.get(quote.SBQQ__Opportunity2__c);
                    quote.ownerid = opp.ownerid;
                quote.CPQ_Approver_Director__c = opp.owner.SPR_Approver_Director_Level__c;
                quote.CPQ_Approver_RVP__c = opp.owner.SPR_Approver_RVP__c;
                quote.CPQ_Approver_GVP__c = opp.owner.SPR_Approver_GVP__c;
                quote.CPQ_Approver_EVP__c = opp.owner.SPR_Approver_EVP__c;
                quote.CPQ_Approver_RD_Regional_Director__c = opp.owner.SPR_Approver_RD_Regional_Director__c;
                quote.CPQ_Approver_GD_Group_Director__c = opp.Owner.SPR_Approver_GD_Group_Director__c;
            }
        }
    }

    public static void processQuoteOnBeforeUpdate(Map<Id, SBQQ__Quote__c> newMap, Map<Id, SBQQ__Quote__c> oldMap){
        for(SBQQ__Quote__c q : newMap.values()) {
            SBQQ__Quote__c oldQ = oldMap.get(q.Id);
            system.debug(q.CPQ_Quote_Remaining_Claim_Status__c  + '_'+ q.CPQ_Quote_Product_Count__c + '_'+oldQ.CPQ_Closed__c );
            if(q.CPQ_Quote_Remaining_Claim_Status__c  <= 0 && q.CPQ_Quote_Product_Count__c > 0 && oldQ.CPQ_Closed__c == false) {
                q.CPQ_Closed__c = true;
            } else {
                q.CPQ_Closed__c = false;
            }
            system.debug(q.CPQ_Closed__c +'_');
        }
    }
    
    public static void processQuoteOnAfterUpdate(Map<Id, SBQQ__Quote__c> newMap, Map<Id, SBQQ__Quote__c> oldMap){
        Set<ID> Idset = new Set<ID>();
        for(SBQQ__Quote__c q : newMap.values()) {
            SBQQ__Quote__c oldQ = oldMap.get(q.Id);
            if(q.CPQ_Quote_Remaining_Claim_Status__c  <= 0 && q.CPQ_Quote_Product_Count__c > 0 && q.CPQ_Closed__c == true && oldQ.CPQ_Closed__c == false) {
                Idset.add(q.Id);
            }
        }

        if(!IdSet.isEmpty()) {
            updateOpportuntiy(IdSet);
        }
    }

    @future
    public static void updateOpportuntiy(Set<Id> Idset){
        Set<Id> oppIdSet = new Set<Id>();
        List<Opportunity> oppLst = new List<Opportunity>();
        system.debug(Idset);
        for(SBQQ__Quote__c q : [select id, name, CPQ_Distributor__r.name, SBQQ__Opportunity2__c, SBQQ__Opportunity2__r.stageName                                               
                                                                                      from SBQQ__Quote__c 
                                                                                      where id in :IdSet and 
                                                                                      (CPQ_Distributor__r.name like 'US -%' Or CPQ_Distributor__r.name like 'CAN -%' OR CPQ_Distributor__r.name like 'LAT -%' )] ){
            if(!oppIdSet.contains(q.SBQQ__Opportunity2__c)) {
                oppIdSet.add(q.SBQQ__Opportunity2__c);
                Opportunity opp = new opportunity(id = q.SBQQ__Opportunity2__c);
                if(q.CPQ_Distributor__C != null) { 
                        If((q.CPQ_Distributor__r.name.Startswith('US -') || q.CPQ_Distributor__r.name.Startswith('CAN -'))
                            &&  (q.SBQQ__Opportunity2__r.stageName != 'P.O. Issued,Fortinet can Ship & Invoice' && q.SBQQ__Opportunity2__r.stageName != 'Closed Won - All Products on Leaderboard')) {
                        opp.stageName = 'P.O. Issued,Fortinet can Ship & Invoice';
                        oppLst.add(opp);
                    }  else If (q.CPQ_Distributor__r.name.Startswith('LAT -') 
                            &&  q.SBQQ__Opportunity2__r.stageName != 'Closed Won - All Products on Leaderboard') {
                        opp.stageName = 'Closed Won - All Products on Leaderboard';
                        oppLst.add(opp);
                    }
                }
            }
            system.debug(oppLst);
            if(!oppLst.isEmpty()) {
                update oppLst;
            }
        }
    }
        /*
    public static void lockAfterApproval(List<SBQQ__Quote__c> newQ, List<SBQQ__Quote__c> oldQ) {
    
    SBQQ__Quote__c blankQuote = new SBQQ__Quote__c();
    Schema.SObjectType objType = blankQuote.getSObjectType();
    Map<String, Schema.SObjectField> fieldMap = Schema.SObjectType.SBQQ__Quote__c.fields.getMap();
        
            
        
    } 
    
        private static void updateSPRAndCategoryMetrics(List<SBQQ__QuoteLine__c> sprProductList, String sprId) {
    
}*/
}
User-added image

Above code is not covered. 

Below is the test class writter it is having only 67% code coverage
@isTest(seealldata=true)
public class CpqQuoteTriggerUtilTest
{  
    public static testMethod void method1(){
        
        Profile profileObj =[select id from Profile where name='System Administrator' limit 1];
        User u = new User(
            FirstName = 'Sudhir',
            LastName = 'Testing Usre',
            Alias = 'tstN',
            Email = 'test11@abc.com',
            Username = 'partne1r11@abc.com',
            CommunityNickname = 'testi11',
            emailencodingkey = 'UTF-8',
            languagelocalekey = 'en_US',
            localesidkey = 'en_US',
            timezonesidkey = 'America/Los_Angeles',
            profileId = profileObj.Id,
            spr_region__c = 'LATAM',
            NFR_User__c  = false,
            SPR_to_Forticare_Linking__c = true
        );
        insert u;
        
        Distributor__c Dist = [select id from Distributor__c where CPQ_DistributorStatus__c  = true limit 1];
        
        Account Pact = [select id from account where recordtype.name = '.Partner' and Is_In_RV_Portal__c = true and Partner_Status__c = 'Activated' limit 1];
        
        Contact Pcnt = [select id from contact where accountid = :Pact.id  limit 1];    
        
        Account Act = new Account( Name = 'Test Sudhir Ac',Website='www.sudhir.com',Industry='Legal',BillingStreet='894', BillingCity='sunnyvalley', BillingState='CA', 
                                  BillingPostalCode='997',BillingCountry='United States',Customer_Status__c='Current Customer');  
        
        insert Act; 
        
        Contact C = [select id from contact limit 1];
        
        Opportunity opp = new Opportunity(
            AccountId=Act.id,
            StageName='Omit from Forecast',
            Amount = 0,
            Name = 'Test Sudhir',
            CloseDate = Date.today(),
            Market_Segmentation__c = 'Education',
            End_User_Industry__c = 'Education',
            End_Customer_Country__c = 'United States',
            Deal_Type__c='Refresh',
            Primary_Opportunity_Contact__c =  c.id,
            Distributor__c = Dist.id
        );
        
        insert opp;
        
        OpportunityTeamMember oppmem = new OpportunityTeamMember(
            Opportunityid = opp.id,
            OpportunityAccessLevel = 'Read' ,
            TeamMemberRole = 'CAM',
            Userid = u.id ); 
        
        
        insert oppmem;
        
        
        
        SBQQ__Quote__c SQ = new SBQQ__Quote__c(SBQQ__Opportunity2__c = opp.id,
                                               CPQ_Distributor__c = Dist.id,
                                               CPQ_Partner_Account__c = Pact.id,
                                               CPQ_Partner_Contact__c = Pcnt.id
                                              );
        
        
        insert SQ;
        
        
        
        SBQQ__Quote__share SQS = new SBQQ__Quote__share(UserOrGroupId = u.id,
                                                        ParentId = SQ.id,
                                                        AccessLevel  = oppmem.OpportunityAccessLevel
                                                       );
        
        insert SQS;
        
        
        
        opp.stageName = 'P.O. Issued,Fortinet can Ship & Invoice';
        opp.Partner_Initiated__c = 'No';
        
        update opp;
        
    }
    
 
    
}

 
Best Answer chosen by MaheemSam
KrishnaAvvaKrishnaAvva
Hi Maheem,

This has been solved here. Please refer to this thread:
https://developer.salesforce.com/forums/?id=906F000000094QOIAY

Please mark this as SOLVED in it had helped you. Thanks!

Regars,
Krishna Avva

All Answers

KrishnaAvvaKrishnaAvva
Hi Maheem,

This has been solved here. Please refer to this thread:
https://developer.salesforce.com/forums/?id=906F000000094QOIAY

Please mark this as SOLVED in it had helped you. Thanks!

Regars,
Krishna Avva
This was selected as the best answer
MaheemSamMaheemSam
Hi Krishna, 
   
   I am trying to call the @future method as suggested  below  but am getting below error 

Error: Compile Error: Method does not exist or incorrect signature: void updateOpportuntiy(Id) from the type CpqQuoteTriggerUtil at line 92 column 29
Test.startTest();

        CpqQuoteTriggerUtil.updateOpportuntiy(SQ.id);

 Test.stopTest();

Please suggest

Thanks
Sudhir
KrishnaAvvaKrishnaAvva
Hi Maheem,

Your class accepts a Set of ID
<code>
updateOpportuntiy(Set<Id> Idset)
</code>
but you are passing a single ID. Hence the error of incorrect signature.

Regards,
Krishna Avva