You need to sign in to do that
Don't have an account?

Help for test class coverage
Hi All,
I have developed an apex class whcih basically assign users to queue dynamically.I have a VF page which will launch from a custom VF tab.This VF page shows few users and with the checkbox.When checkbox against the username is marked and hit Save button then seleected users will go to the queue defined in class.
Below is my clas:
I am not able to cover below lines of code:

My current code coverage stands at 73%
Many I request to help me on same to get a code coverage above 75%
Many thanks in advance
Thanks & Regards,
Harjeet
I have developed an apex class whcih basically assign users to queue dynamically.I have a VF page which will launch from a custom VF tab.This VF page shows few users and with the checkbox.When checkbox against the username is marked and hit Save button then seleected users will go to the queue defined in class.
Below is my clas:
public class TodayTelesalesDetails { public Date Today { get { return Date.today(); } } public List<wrapAgent> wrapAgentList{get; set;} public List<User> selectedAgents{get;set;} public TodayTelesalesDetails (){ if(wrapAgentList == null) { wrapAgentList = new List<wrapAgent>(); for(User teleSalesAgent: [select Id, Name, Email from User where Name IN('Steve Waugh','Yuvraj Singh')]) { // As each agent is processed we create a new wrap object and add it to the wrapAgentList wrapAgentList.add(new wrapAgent(teleSalesAgent)); } } } public PageReference doFullSave(){ set<id> userSetId = new set<id>(); list<GroupMember> gmm = new list<GroupMember>(); List<GroupMember> toBeDeleted= [SELECT Id, GroupId, UserOrGroupId FROM GroupMember where GroupId = '00G9D000000toPYUAY']; delete toBeDeleted; List<User> userlist=[select Id, Name from User where Name IN('Steve Waugh','Yuvraj Singh')]; for(User u : userlist) { system.debug('message3>>'+u); for(wrapAgent wrapAccountObj : wrapAgentList) { if(wrapAccountObj.selected == true) { userSetId.add(wrapAccountObj.teleAgent.id); } } } for(User us : [select id from User where id =: userSetId]) { for(Group gg : [select id,name from Group where type = 'Queue' and Name = 'TeleSales Queue']) { GroupMember gm = new GroupMember(); gm.groupId = gg.id; gm.UserOrGroupId = us.Id; gmm.add(gm); } } insert gmm; PageReference pageRef = new PageReference('/'); pageRef.setRedirect(true); return pageRef; } public PageReference doCancel(){ PageReference pageRef = new PageReference('/'); pageRef.setRedirect(true); return pageRef; } public class wrapAgent{ public User teleAgent {get; set;}//Please put the custom metadata names public Boolean selected {get; set;} //This is the contructor method. When we create a new object we pass a telesales metadata member that is set to the acc property. We also set the selected value to false public wrapAgent(User teleSalesAgent) { teleAgent = teleSalesAgent; selected = false; } } }and below is my test class:
@isTest public class Test_TodayTeleSalesDetails { public static testMethod void validateusersinqueue() { Group testGroup = new Group(Name='test group', Type='Queue'); insert testGroup; System.runAs(new User(Id=UserInfo.getUserId())) { QueuesObject testQueue = new QueueSObject(QueueID = testGroup.id, SObjectType = 'Lead'); insert testQueue; } Test.startTest(); TodayTelesalesDetails telesalesDetails=new TodayTelesalesDetails(); telesalesDetails.doFullSave(); telesalesDetails.doCancel(); Test.stopTest(); } }
I am not able to cover below lines of code:
for(User us : [select id from User where id =: userSetId]) { system.debug('message6>>'+us); for(Group gg : [select id,name from Group where type = 'Queue' and Name = 'TeleSales Queue']) { GroupMember gm = new GroupMember(); gm.groupId = gg.id; gm.UserOrGroupId = us.Id; gmm.add(gm); } }Below is the screenshot for the same:
My current code coverage stands at 73%
Many I request to help me on same to get a code coverage above 75%
Many thanks in advance
Thanks & Regards,
Harjeet
If you want to keep "selected" of wrapAgent as false and want to increase your code coverage then you can use Test.isRunningtest() in your code. You can update your code as below:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
All Answers
the provided code is very helpful for my task. Thanks for sharing this info.
Thanks
Umsh Kant
Wismad.com
Best Web Development Company (https://www.wismad.com)
I have tried your code in my Org and found that the value of the 'selected' of wrapper named wrapAgent is always set to false and due to this 'userSetId' set is not updating with the userId which you are further using in your 'For loop'.
You can set it to true as I did or you can dynamically set the 'true' or 'false' for different scenarios as per your requirement. Below code is giving 92% code coverage in my Org.
Also, In the test class you need to set the value of name as 'TeleSales Queue' instead of 'test group' while inserting the Group.
Apex class :
============================================================
Test class :
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Just a side note:I always read your blog on Salesforce. Keep up the good work. Inspires a lot many people for doing good work in Salesforce
I tried the code as suggested by you. There are 2 aspects
1. If I marked "selected" of wrapAgent as true and ran the test class I got test coverage of 92%. But when I checked at the frontend I found that all the checkboxes were by default checked.Ideally it should not be the case.We need to allow user to check the checkboxes
2. I kept "selected" of wrapAgent as false only and ran the test class still the test coverage stands at 73%
Kindly advise or provide inputs Isn't there a way we can have coverage of wrapper selected is false
Many thanks in advance
If you want to keep "selected" of wrapAgent as false and want to increase your code coverage then you can use Test.isRunningtest() in your code. You can update your code as below:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com