-3

Below is my class but i am not able to cover single line also , can anyone help me out in this test class, i am not understanding why i am not able to cover single line.

global class OppTerrAssignDefaultLogicFilter implements TerritoryMgmt.OpportunityTerritory2AssignmentFilter 
{
    global OppTerrAssignDefaultLogicFilter() {}
    global Map<Id,Id> getOpportunityTerritory2Assignments(List<Id> opportunityIds) {
        Map<Id, Id> OppIdTerritoryIdResult = new Map<Id, Id>();
        // Get the active territory model Id
        Id activeModelId = getActiveModelId();
        if(activeModelId != null){
            List<Opportunity> opportunities = [Select Id, AccountId, Territory2Id from Opportunity where Id IN :opportunityIds];
            Set<Id> accountIds = new Set<Id>();
            // Create set of parent accountIds
            for(Opportunity opp:opportunities){
                if(opp.AccountId != null){
                    accountIds.add(opp.AccountId);
                }
            }
            Map<Id,Territory2Priority> accountMaxPriorityTerritory = getAccountMaxPriorityTerritory(activeModelId, accountIds);
            // For each opportunity, assign the highest priority territory if there is no conflict, else assign null.
            for(Opportunity opp: opportunities){
                Territory2Priority tp = accountMaxPriorityTerritory.get(opp.AccountId);
                // Assign highest priority territory if there is only 1.
                if((tp != null) && (tp.moreTerritoriesAtPriority == false) && (tp.territory2Id != opp.Territory2Id)){
                    OppIdTerritoryIdResult.put(opp.Id, tp.territory2Id);
                }else{
                    OppIdTerritoryIdResult.put(opp.Id, null);
                }
            }
        }
        return OppIdTerritoryIdResult;
    }
    /**
* Query assigned territoryIds in active model for given accountIds.
* Create a map of accountId to max priority territory.
*/
    private Map<Id,Territory2Priority> getAccountMaxPriorityTerritory(Id activeModelId, Set<Id> accountIds){
        Map<Id,Territory2Priority> accountMaxPriorityTerritory = new Map<Id,Territory2Priority>();
        for(ObjectTerritory2Association ota:[Select ObjectId, Territory2Id, Territory2.Territory2Type.Priority from ObjectTerritory2Association where objectId IN :accountIds and Territory2.Territory2ModelId = :activeModelId]){
            Territory2Priority tp = accountMaxPriorityTerritory.get(ota.ObjectId);

            if((tp == null) || (ota.Territory2.Territory2Type.Priority > tp.priority)){
                //If this is the first territory examined for account or it has greater priority than current highest priority territory, then set this as new highest priority territory
                tp = new Territory2Priority(ota.Territory2Id,ota.Territory2.Territory2Type.priority,false);
            }else if(ota.Territory2.Territory2Type.priority == tp.priority){
                // The priority of current highest territory is same as this, so set moreTerritoriesAtPriority to indicate multiple highest priority territories seen so far.
                tp.moreTerritoriesAtPriority = true;
            }
            accountMaxPriorityTerritory.put(ota.ObjectId, tp);
        }
        return accountMaxPriorityTerritory;
    }
    /**
* Get the Id of the Active Territory Model.
* If none exists, return null.
*/

    private Id getActiveModelId() {
        List<Territory2Model> models = [Select Id from Territory2Model where State = 'Active'];
        Id activeModelId = null;
        if(models.size() == 1){
            activeModelId = models.get(0).Id;
        }
        return activeModelId;
    }
    /**
* Helper class to help capture territory2Id, its priority, and whether there are more territories with same priority assigned to the account.
*/


    private class Territory2Priority {
        public Id territory2Id { get; set; }
        public Integer priority { get; set; }
        public Boolean moreTerritoriesAtPriority { get; set; }
        Territory2Priority(Id territory2Id, Integer priority, Boolean moreTerritoriesAtPriority){
            this.territory2Id = territory2Id;
            this.priority = priority;
            this.moreTerritoriesAtPriority = moreTerritoriesAtPriority;
        }
    }
}

Test class:

@istest
private class OppTerrAssignDefaultFiltertest{
    private static testmethod void oppTerrAssigntest(){

        List<id> opplistid=new List<id>();
       User u;
        Account acc; 
        User thisUser = [SELECT Id FROM User where id=:UserInfo.getUserId()];  
        System.runAs (thisUser) {
            Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
            //UserRole r = [SELECT Id FROM UserRole WHERE Name='Testing'];
            u = new User(alias = 'jsmith', email='[email protected]',
                         emailencodingkey='UTF-8', lastname='Smith',
                         languagelocalekey='en_US',localesidkey='en_US', 
                         profileid = p.Id, timezonesidkey='America/Los_Angeles',
                         username='[email protected]');
            insert u;
            Set<id> acclistid=new Set<id>();
            acc=new Account();
            acc.Name='acc1';
            acc.BillingCity ='Chennai' ;
            acc.BillingCountry='United States';
            acc.BillingLatitude=5.5;
            acc.BillingLongitude=7.6;
            acc.BillingPostalCode='600075';
            acc.BillingState='Armed Forces Americas';
            acc.BillingStreet='water well street';  
            insert acc;  

            opportunity opp =new opportunity(name='testopporunity',StageName='Discovery',CloseDate=system.today(),accountid=acc.id,Amount=12);
            insert opp;
            opplistid.add(acc.id);

            List<Territory2Type> terriType  = [SELECT id, DeveloperName from Territory2Type where  DeveloperName = 'TestTerrirtarys' ];

           if(terriType.size()>0)
           {
                Territory2Model terrModel = new Territory2Model();
                terrModel.DeveloperName='TestTerrirtarys1';
                terrModel.Name = 'TestTerrirtarys2';
                insert terrModel ;

                Territory2 objTerr = new Territory2(DeveloperName = 'TestTerritory', Territory2ModelId=terrModel.Id, Name='TestTerritory', Territory2TypeId=terriType[0].Id);

                insert objTerr;

                OppTerrAssignDefaultLogicFilter CheckOppty=new OppTerrAssignDefaultLogicFilter();
                Map<Id,Id> mapid=CheckOppty.getOpportunityTerritory2Assignments(opplistid);
                //Map<Id,Territory2Priority>MapTerritory=CheckOppty.getAccountMaxPriorityTerritory(objTerr.Id, acclistid);
                Id id=CheckOppty.getActiveModelId();
                OppTerrAssignDefaultLogicFilter.Territory2Priority innerclass=new OppTerrAssignDefaultLogicFilter.Territory2Priority(objTerr.id,9908,true);
                //innerclass.territory2Id;
                //innerclass.priority;
                //innerclass.moreTerritoriesAtPriority;
                Test.stoptest();
            }
        }
    }
}
2
  • @VijayGanji: i am not understanding how to cover that, as i test class getting passed but not covering single line, i dont know where i am doing mistake
    – user72615
    Sep 3, 2019 at 10:11
  • 1
    You need to check each logical branch to make sure it accesses the data you think it accesses. An immediate suspect is if(terriType.size()>0). If your SOQL query there is wrong or returns no result, your test just won't execute.
    – David Reed
    Sep 3, 2019 at 12:20

1 Answer 1

0

Not sure why you're not getting a single line of coverage, but you have a bug after insert opp; in your test method.

You are doing:

insert opp;
opplistid.add(acc.id);

Looks like you want to actually be adding the newly created Opportunity Id rather than the Account Id. Use this instead:

opplistid.add(opp.id);

Try updating and see if that helps

2

Not the answer you're looking for? Browse other questions tagged .