+ Start a Discussion
SFDC n12SFDC n12 

Test class coverage help needed

Hi,

I have the following class for which i have written the test class which covers only 66% , not able to figure out how to cover t he rest 

Controller :

public with sharing class AF_FrozenUserExtension {

   private List<User> frozenUsers {get; set;}
   private Map<String,User> userMap = new Map<String,User>();
   private Map<String,List<Task>> usrTaskMap = new Map<String,List<Task>>();
   private Map<String,List<Opportunity>> usrOppMap = new Map<String,List<Opportunity>>();
   private Map<String,List<Threat__c>> usrThrtMap = new Map<String,List<Threat__c>>();
   public List<FrozenUser> frozenUserList{get; set;}

  
    public AF_FrozenUserExtension() {
    }
   
    public Class FrozenUser{
        public String UserId{get;set;}
        public String usrName{get;set;}
        public Integer taskCount{get;set;}
        public Integer oppCount{get;set;}
        public Integer thrtCount{get;set;}
       
        public FrozenUser(String UserId,Integer taskCount,Integer oppCount,Integer thrtCount,String usrName){
            this.UserId = UserId;
            this.taskCount = taskCount;
            this.oppCount = oppCount;
            this.thrtCount = thrtCount;
            this.usrName = usrName;
        }
    }
    public void getFrozenUsers(){
   
        frozenUsers = new List<User>();
      for (User u : [SELECT Id,LastModifiedDate ,Name FROM User WHERE Id in (Select userId  From UserLogin WHERE IsFrozen = TRUE) AND IsActive=true]) {
         frozenUsers.add (u);
         userMap.put(u.Id,u);
          }
  
      
       //logic to display the open tasks
       for(Task tsk: [SELECT ownerId FROM Task WHERE  Status!='Completed' AND OwnerId in :userMap.keySet()])
       {
            if(usrTaskMap.containsKey(tsk.ownerId)){
                usrTaskMap.get(tsk.ownerId).add(tsk);
            }
            else{
                List<Task> tskList = new List<Task>();
                tskList.add(tsk);
                usrTaskMap.put(tsk.ownerId,tskList);
            }
       }
      

      
       //logic to display open opportunity
       for(Opportunity op :[SELECT ownerId FROM Opportunity WHERE  StageName NOT IN ('Closed Won', 'Closed Lost') AND OwnerId in :userMap.keySet()])
       {
            if(usrOppMap.containsKey(op.ownerId)){
                usrOppMap.get(op.ownerId).add(op);
            }
            else{
                List<Opportunity> opList = new List<Opportunity>();
                opList.add(op);
                usrOppMap.put(op.ownerId,opList);
            }
       }

      
      //logic to display open threats
       for(Threat__c thrt: [Select ownerId from Threat__c where Status__c NOT IN ('Closed Won', 'Closed Lost')AND OwnerId in :userMap.keySet()])
       {
            if(usrThrtMap.containsKey(thrt.ownerId)){
                usrThrtMap.get(thrt.ownerId).add(thrt);
            }
            else{
                List<Threat__c> thrtList = new List<Threat__c>();
                thrtList.add(thrt);
                usrThrtMap.put(thrt.ownerId,thrtList);
            }
       }
      
        if(frozenUsers != null && frozenUsers.size() >0){
            FrozenUser frnzUsr;
            frozenUserList = new List<FrozenUser>();
           
           
            for(User usr: frozenUsers)
            {
                Integer taskCount =0,oppCount=0,thrtCount=0;
                if(usrTaskMap.containsKey(usr.Id)){
                    taskCount = usrTaskMap.get(usr.Id).size();
                }
                if(usrOppMap.containsKey(usr.Id)){
                    oppCount  = usrOppMap.get(usr.Id).size();
                }
                if(usrThrtMap.containsKey(usr.Id)){
                    thrtCount = usrThrtMap.get(usr.Id).size();
                }
               
                frnzUsr = new FrozenUser(usr.Id,taskCount,oppCount,thrtCount,usr.Name);
                frozenUserList.add(frnzUsr);
            }
        }
     
  }
 
  }


Test Class :

/**
*
*/
@isTest


private class Test_FrozenUserExtension {
static testMethod void myUnitTest() {
AF_FrozenUserExtension frozenusers = new AF_FrozenUserExtension();
frozenusers.getFrozenUsers();
}
}

I have called the method getFrozenUsers() directly with my class instance which covers the major portion, not sure how to cover the above part.

Help me on this regard

Thanks in Advance
bob_buzzardbob_buzzard
You have to give us more to work with than this - we aren't the Salesforce platform so we can't just figure out which lines aren't being covered by your test - you need to tell us where the gaps are.
SFDC n12SFDC n12
Yup,

Actually my class is a wrapper class 



public with sharing class AF_FrozenUserExtension {

   private List<User> frozenUsers {get; set;}
   private Map<String,User> userMap = new Map<String,User>();
   private Map<String,List<Task>> usrTaskMap = new Map<String,List<Task>>();
   private Map<String,List<Opportunity>> usrOppMap = new Map<String,List<Opportunity>>();
   private Map<String,List<Threat__c>> usrThrtMap = new Map<String,List<Threat__c>>();
   public List<FrozenUser> frozenUserList{get; set;}

 
    public AF_FrozenUserExtension() {
    }
  
    public Class FrozenUser{
        public String UserId{get;set;}
        public String usrName{get;set;}
        public Integer taskCount{get;set;}
        public Integer oppCount{get;set;}
        public Integer thrtCount{get;set;}
      
        public FrozenUser(String UserId,Integer taskCount,Integer oppCount,Integer thrtCount,String usrName){
            this.UserId = UserId;
            this.taskCount = taskCount;
            this.oppCount = oppCount;
            this.thrtCount = thrtCount;
            this.usrName = usrName;
        }
    }


//Method called in my test class


    public void getFrozenUsers(){
  
        frozenUsers = new List<User>();
      for (User u : [SELECT Id,LastModifiedDate ,Name FROM User WHERE Id in (Select userId  From UserLogin WHERE IsFrozen = TRUE) AND IsActive=true]) {
         frozenUsers.add (u);
         userMap.put(u.Id,u);
          }
 
     
       //logic to display the open tasks
       for(Task tsk: [SELECT ownerId FROM Task WHERE  Status!='Completed' AND OwnerId in :userMap.keySet()])
       {
            if(usrTaskMap.containsKey(tsk.ownerId)){
                usrTaskMap.get(tsk.ownerId).add(tsk);
            }
            else{
                List<Task> tskList = new List<Task>();
                tskList.add(tsk);
                usrTaskMap.put(tsk.ownerId,tskList);
            }
       }
     

     
       //logic to display open opportunity
       for(Opportunity op :[SELECT ownerId FROM Opportunity WHERE  StageName NOT IN ('Closed Won', 'Closed Lost') AND OwnerId in :userMap.keySet()])
       {
            if(usrOppMap.containsKey(op.ownerId)){
                usrOppMap.get(op.ownerId).add(op);
            }
            else{
                List<Opportunity> opList = new List<Opportunity>();
                opList.add(op);
                usrOppMap.put(op.ownerId,opList);
            }
       }

     
      //logic to display open threats
       for(Threat__c thrt: [Select ownerId from Threat__c where Status__c NOT IN ('Closed Won', 'Closed Lost')AND OwnerId in :userMap.keySet()])
       {
            if(usrThrtMap.containsKey(thrt.ownerId)){
                usrThrtMap.get(thrt.ownerId).add(thrt);
            }
            else{
                List<Threat__c> thrtList = new List<Threat__c>();
                thrtList.add(thrt);
                usrThrtMap.put(thrt.ownerId,thrtList);
            }
       }
     
        if(frozenUsers != null && frozenUsers.size() >0){
            FrozenUser frnzUsr;
            frozenUserList = new List<FrozenUser>();
          
          
            for(User usr: frozenUsers)
            {
                Integer taskCount =0,oppCount=0,thrtCount=0;
                if(usrTaskMap.containsKey(usr.Id)){
                    taskCount = usrTaskMap.get(usr.Id).size();
                }
                if(usrOppMap.containsKey(usr.Id)){
                    oppCount  = usrOppMap.get(usr.Id).size();
                }
                if(usrThrtMap.containsKey(usr.Id)){
                    thrtCount = usrThrtMap.get(usr.Id).size();
                }
              
                frnzUsr = new FrozenUser(usr.Id,taskCount,oppCount,thrtCount,usr.Name);
                frozenUserList.add(frnzUsr);
            }
        }
    
  }


  }



i have called the method getFrozen users(), which covers the most of the portion


The following logic need to be covered


public Class FrozenUser{
        public String UserId{get;set;}
        public String usrName{get;set;}
        public Integer taskCount{get;set;}
        public Integer oppCount{get;set;}
        public Integer thrtCount{get;set;}
     
        public FrozenUser(String UserId,Integer taskCount,Integer oppCount,Integer thrtCount,String usrName){
            this.UserId = UserId;
            this.taskCount = taskCount;
            this.oppCount = oppCount;
            this.thrtCount = thrtCount;
            this.usrName = usrName;
        }
    }


need help to cover the above lines of code


My test class


/**
*
*/
@isTest

private class Test_FrozenUserExtension {
static testMethod void myUnitTest() {
AF_FrozenUserExtension frozenusers = new AF_FrozenUserExtension();
frozenusers.getFrozenUsers();
}
}


Thanks in Advance



bob_buzzardbob_buzzard
This implies that you don't have any frozen users that your test can pick up on.  You'll need to setup some test data in that state. What I would do is execute a SOQL query to find a user that isn't that isn't the user executing the text, then freeze them, then create your controller.  Something like:

User u=[select id from User where IsActive=true AND id!=:UserInfo.getUserID() limit 1];
UserLogin ul=[select id, IsFrozen from UserLogin where UserId=:u.id];
ul.IsFrozen=true;
update ul;
You may need to add aTest.startTest() after this, as Apex doesn't like you accessing setup and non-setup information in the same transaction.

SFDC n12SFDC n12
No, There are frozen users in my system , there are around 4 of them who are in frozen state 
SFDC n12SFDC n12
how to cover my inner class part in my test class , thats the place i am having issues, not sure how to cover my inner class


public Class FrozenUser{
        public String UserId{get;set;}
        public String usrName{get;set;}
        public Integer taskCount{get;set;}
        public Integer oppCount{get;set;}
        public Integer thrtCount{get;set;}
    
        public FrozenUser(String UserId,Integer taskCount,Integer oppCount,Integer thrtCount,String usrName){
            this.UserId = UserId;
            this.taskCount = taskCount;
            this.oppCount = oppCount;
            this.thrtCount = thrtCount;
            this.usrName = usrName;
        }
    }




SvenSven
Still in your test class you need create test data like @bob_buzzard said.
By just calling your method is not enough to do the test.

So if you query the users in your system and put one to frozen state and then call your method then this will be a better result then just calling your method.
for a proper test you also need to do this with Threat__c and Opportunity
SFDC n12SFDC n12
i have 4 frozen users in my system already present, 

i have used the query in the test class

User u=[select id from User where IsActive=true AND id!=:UserInfo.getUserID() limit 1];
UserLogin ul=[select id, IsFrozen from UserLogin where UserId=:u.id];
ul.IsFrozen=true;
update ul;


should i log in as frozen user and then run the test class ?
bob_buzzardbob_buzzard
No. Once that user is setup correctly, the query will match them and the wrapper class will be created.  If there are still problems covering, that means your queries aren't working as you expect and you'll need to debug them.
bob_buzzardbob_buzzard
"i have 4 frozen users in my system already present,"

If this is really the case then your code isn't working correctly, as it isn't finding them and creating a wrapper for each.