You need to sign in to do that
Don't have an account?
SFDC 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
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
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
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.
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;
}
}
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
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 ?
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.