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
bonny mankotiabonny mankotia 

Test Class Code Coverage Show Only 66%

Hello Guys I need a Test Class for Below Class Please help me.

public with sharing class Accountcontactpicklist 
{
    public String selectedAccId{get;set;}
    public String selectedConId{get;set;}
      
          public List<SelectOption> getAccountNames()
           {
                  List<SelectOption> accOptions= new List<SelectOption>();
                  accOptions.add( new SelectOption('','--Select--'));
                  for( Account acc : [select Id,name from Account where name like '[abc]%' limit 5 ] ) 
                  {
                          accOptions.add( new SelectOption(acc.Id,acc.name));
                  }
                 
                 return accOptions;
           }
         
           public List<SelectOption> getContactNames() 
           {
                  
                  List<SelectOption> conOptions= new List<SelectOption>();
                  List<SelectOption> options = new List<SelectOption>();
                    if(selectedAccId != null)
                    {     
                       for( contact con : [select Id,name,accountid from contact where accountid=:selectedAccId ] ) 
                       {
                          conOptions.add( new SelectOption(con.Id,con.name));
                       }
                    }                  
                    else
                    {
                        conOptions.add( new SelectOption('--None--','--None--'));
                    }
                 return conOptions;
           }
    }
Abhishek_DEOAbhishek_DEO
With below test class, you should be able to cover 100% of above code. Trick is that you need to call getter property specifically
 
@isTest
public class Tracker_Accountcontactpicklist
{
 private static testMethod void testgetAccountNames()
 {
    Account acc = new account(name='[abc]%');
    insert acc; 
    Accountcontactpicklist picklistcontroller = new Accountcontactpicklist();
    picklistcontroller.selectedAccId = acc.Id;
    List<SelectOption> sp = picklistcontroller.getAccountNames();
    system.assertEquals(sp.size(),2);
  
 }

  private static testMethod void testgetContactNames()
 {
 
    Account acc = new account(name='[abc]');
    insert acc;
    contact con = new contact (accountid=acc.id, Firstname='test', LastName='test');
    insert con;

    Accountcontactpicklist contactController = new Accountcontactpicklist();
    List<SelectOption> sp1 = contactController.getContactNames();
    system.assertEquals(sp1.size(),1);
    
    contactController.selectedAccId = acc.id;
    contactController.selectedConId = con.id;
    string getselectedAccId = contactController.selectedAccId;
    string getselectedConId = contactController.selectedConId;
    List<SelectOption> sp2 = contactController.getContactNames();
    system.assertEquals(sp2.size(),1);
   
 }

}

 
Abhishek_DEOAbhishek_DEO

Hi,

Did you try above code?
Please mark it as best answer if it helps you.

Thanks,

Abhishek

bonny mankotiabonny mankotia
Thanks Abhisekh.
Abhishek_DEOAbhishek_DEO
Bonny, if it helped you please mark this as best answer so that others can take reference in case of same situation.