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
chubsubchubsub 

Test Method to cover Wrapper Class only covering 79%

I'm only getting 79% coverage, the lines in red aren't cover, any ideas on how to increase my coverage?  Thanks!

 

Test Method:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@isTest
private class wrapperClassControllerTest {

static testmethod void wrapperClassController()
  
  {
  
 Profile p = [select id from profile where name='Standard User'];
  User user=new User(alias = 'test123', email='test123@noemail.com',
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p.Id, country='United States',
            timezonesidkey='America/Los_Angeles', username='test123@noemail.com');
        insert user;     

Account account = new Account();// specify all the required fields
account.name = 'testing';
insert account;
Contact contact = new Contact();// specify all the required fields
contact.lastname = 'test';
insert contact;
Case ca = new Case();
ca.accountId = account.Id;
ca.contactId = contact.Id;
insert ca;

 string userIndex=user.ID; 

        
        ApexPages.StandardController controller = new ApexPages.StandardController(ca);
        wrapperClassController sc = new wrapperClassController(controller);      
         Pagereference S = sc.processSelected();
         
 
 system.assertEquals(3,sc.userList.size());   
 
 sc.userList=sc.getUsers();  
 sc.userList[0].selected=true;    


       }

    }

 

 

 

Class:

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
public class wrapperClassController {

    public wrapperClassController(ApexPages.StandardController controller) {

    }
    
    //Our collection of the class/wrapper objects cUser 
    public List<cUser> userList {get; set;}



    //This method uses a simple SOQL query to return a List of Users
   
    public List<cUser> getUsers() {
        if(userList == null) {
            userList = new List<cUser>();
            for(User u: [select Id, Name, Email, Phone from User]) {
                // As each contact is processed we create a new cContact object and add it to the contactList
                userList.add(new cUser(u));
            }
        }
        return userList;
    }

    public PageReference processSelected() {

                //We create a new list of Users that we be populated only with Users if they are selected
        List<User> selectedUsers = new List<User>();
      

        //We will cycle through our list of cUsers and will check to see if the selected property is set to true, if it is we add the User to the selectedUsers list
        for(cUser cUse: getUsers()) {
            if(cUse.selected == true) {
                selectedUsers.add(cUse.use);
            }
        }

        // Now we have our list of selected users and can add them to the User notification object on Cases
       

      
      for(User us : selectedUsers) {
      
             // Create new Case Associate Record and Insert
         Case_Associate__c newCaseAssociateRecord = new Case_Associate__c();
         newCaseAssociateRecord.User__c =  us.ID;
         newCaseAssociateRecord.Case__c = (''+ApexPages.currentPage().getParameters().get('id'));
         
         insert newCaseAssociateRecord;  
   
      }
       // Redirect back to case record   
              
      return new PageReference('/'+ApexPages.currentPage().getParameters().get('id'));
        
    }
    // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. 
    // In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
    
    public class cUser {
        public User use {get; set;}
        public Boolean selected {get; set;}

        //This is the contructor method. When we create a new cUser object we pass a User that is set to the 'use' property. We also set the selected value to false
        public cUser(User u) {
            use = u;
            selected = false;
        }
    }
    
  
    
}

Best Answer chosen by Admin (Salesforce Developers) 
UVUV

You should interate through each item of your wrapperlist from your test class and make selected as true for few of them to cover those red lines.

All Answers

UVUV

You should interate through each item of your wrapperlist from your test class and make selected as true for few of them to cover those red lines.

This was selected as the best answer
chubsubchubsub

Thanks again UV, I got full coverage with this:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@isTest
private class wrapperClassControllerTest {

static testmethod void wrapperClassController()
  
  {
  
 Profile p = [select id from profile where name='Standard User'];
  User user=new User(alias = 'test123', email='test123@noemail.com',
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p.Id, country='United States',
            timezonesidkey='America/Los_Angeles', username='test123@noemail.com');
        insert user;     

Account account = new Account();// specify all the required fields
account.name = 'testing';
insert account;
Contact contact = new Contact();// specify all the required fields
contact.lastname = 'test';
insert contact;
Case ca = new Case();
ca.accountId = account.Id;
ca.contactId = contact.Id;
insert ca;

 string userIndex=user.ID; 
 
    PageReference pageRef = Page.caseuserselect;       
    pageRef.getParameters().put('id' , ca.id);
    Test.setCurrentPageReference(pageRef);
      
        ApexPages.StandardController controller = new ApexPages.StandardController(ca);
        wrapperClassController sc = new wrapperClassController(controller); 
        
    sc.userList = sc.getusers();
    sc.userList[1].selected=True; 
    sc.processSelected();
        
       }

    }