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
Ritika JRitika J 

Increase the test coverage

Hi

 

I have to increase the coverage for below class:

public with sharing class ContactPortalUserController
{
    List<User>luser  = new List<User>();
    List<contact>selectedCon = new List<contact>();
    ID profileID = [select id from profile where name = 'High Volume Customer Portal'].id;
    private ApexPages.StandardSetController standardController;
   // ApexPages.Message myMsg = new ApexPages.Message('info', summary);
    public ContactPortalUserController(ApexPages.StandardSetController standardController)
    {
        this.standardController = standardController;
          }

     public PageReference selectedContact(){
     List<Contact>selectedContact = (List<Contact>) standardController.getSelected();
    system.debug('****1***'+selectedContact );
     selectedCon = [select id , name , firstname, email ,EmailEncoding__c,LanguageLocaleKey__c,TimeZoneSidKey__c,LocaleSidKey__c, lastname from contact where id in:selectedContact];
     system.debug('***21***'+selectedCon );
     for(contact con:selectedCon)
     {
      User us = new User();
      us.alias  =   con.Firstname.substring(0,3) +'cus';
      us.email  =  con.email;
      us.emailencodingkey= con.EmailEncoding__c;
      us.lastname = con.lastname;
      us.languagelocalekey = con.LanguageLocaleKey__c;
      us.localesidkey =con.LocaleSidKey__c;
      us.profileid = profileID;
      us.contactId = con.id;
       us.timezonesidkey = con.TimeZoneSidKey__c;
       us.username =  con.email +'cus';
       luser.add(us);
     
     }
    
    try{
     
     insert luser;
     ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Info, 'Users have been inserted successfully.');
    ApexPages.addmessage(myMsg);
     
     // myMsg = ;
                  //ApexPages.addMessage(myMsg);   
     
     }
     catch(exception e)
     {
       e.getmessage();
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error Occured while inserting users,' + e.getmessage());
                  ApexPages.addMessage(myMsg);   
     }
   
     return null;
     }

    
}

 

Highlighted lines are not getting covered.

 

Test class:

 

public class  ContactPortalUserControllerTest{

    public static testMethod void testContactPortalUserController() {
      Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
      User u = new User(Alias = 'standt', Email='standarduser@testorg.com',
      EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
      LocaleSidKey='en_US', ProfileId = p.Id,
      TimeZoneSidKey='America/Los_Angeles', UserName='standarduser1112@testorg.com');


      System.runAs(u) {
    ID profileID = [select id from profile where name = 'High Volume Customer Portal'].id;

    List<contact> lcontact = new List<contact>();
    Contact con = new contact();
    con.lastname = 'test';
    con.email = 'Test@accenture.com';
    con.LanguageLocaleKey__c =  'en_US';
    con.LocaleSidKey__c = 'en_US';
    con.EmailEncoding__c = 'UTF-8';
    con.TimeZoneSidKey__c = 'America/Los_Angeles';
    insert con;
     Contact con1 = new contact();
    con1.lastname = 'test1';
    con1.email = 'Test@accenture.com';
    con1.LanguageLocaleKey__c =  'en_US';
    con1.LocaleSidKey__c = 'en_US';
    con1.EmailEncoding__c = 'UTF-8';
    con1.TimeZoneSidKey__c = 'America/Los_Angeles';
    insert con1;

    lcontact.add(con);
    lcontact.add(con1);

       PageReference pageRef = Page.ContactPortalUser;
        Test.setCurrentPage(pageRef);
          system.debug('*****1****' +  lcontact);     
     ApexPages.StandardSetController sc = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Id,Name FROM contact where id =:lcontact]));
ContactPortalUserController controller = new  ContactPortalUserController(sc);
  controller.selectedContact();
System.assert
      (ApexPages.getMessages().size() == 1);
      }
                    }
}

Jay EcklesJay Eckles

If those lines aren't getting executed, it's because the selectedCon list is empty.  That means zero records are coming back from the query that populates the list.  My best guess for that is that selectedContact list is empty.  That would mean getSelected() is returning null.  It sounds like you need to create a selected element in your test case so that things start populating.