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
ASFASF 

Not passing the select option value in test class

hi,

  Can any one help me to pass the value in select option

Apex Class:

public class HousingOpportunity
{
    public Housing_Resource_Opportunity__c objHrsOpp {get; set;}
    //private String soql{get;set;}
    public HousingOpportunity()
    {
        objHrsOpp = new Housing_Resource_Opportunity__c();
   
    }
   
   public string selectBuildingname{get;set;} 

    public list<selectOption> getBuildingname()
    {
        list<selectOption> options = new list<selectOption>();
        options.add(new selectOption('','-None-'));
        list<Housing_Resource__c> houres= [Select Id,Name from Housing_Resource__c]; 
        Set<String> strUniq = new Set<String>();        
        for(Housing_Resource__c hr : houres)
        {
             if(strUniq.add(hr.Name.touppercase()))
             {
               options.add(new selectOption(hr.Name,hr.Name));
               system.debug('BuildingName--------------'+options);
             }
        }
       
        return options;
    }
 
     public string selectroomnameandnumber{get;set;}
     public List<selectOption> getroomnameandnumber()
     {
  
        List<selectOption> options = new List<selectOption>();
        options.add(new selectOption('','-None-'));
         system.debug('selectBuildingname@@@@@@@@@@@@@@@@@@@@@'+selectBuildingname);
        if(selectBuildingname != null)        // Its not covered from this line  value passing is null
        {

        String hrsSQL = 'Select Room_Name_and_Number__c from Housing_Resource__c  where Name=:selectBuildingname AND Room_Name_and_Number__c!=null';
            List<Housing_Resource__c> houres = Database.query(hrsSQL);
             //system.debug('roomnameupdown'+hrsSQL);
            for(Housing_Resource__c hr : houres)     
            {
                options.add(new selectOption(hr.Room_Name_and_Number__c,hr.Room_Name_and_Number__c ));  
            }
           
           
       } 
    
       return options;
     }
}


Test class:


@isTest
public class HousingOpportunityTestClass
{
    public static TestMethod void HousingOpportunityTestClass()
    {
         PageReference ref = Page.HousingOpportunity;
        Test.setCurrentPage(ref);
        system.debug('Test---------'+ref);

        Housing_Resource__c HousingResouce = new Housing_Resource__c(Name ='TestBuildName', Room_Name_and_Number__c = 'BN101');
        insert HousingResouce;
        
     
            ApexPages.currentPage().getParameters().put('selectBuildingname',HousingResouce.Name); 
           
            HousingOpportunity thehousopp = new HousingOpportunity();
        
      
         thehousopp.getBuildingname();
    
         thehousopp.getroomnameandnumber();

 }
 
}
Best Answer chosen by ASF
Subhash GarhwalSubhash Garhwal
Hi,
In your test class you need to pass "selectBuildingname" string value Like :-
HousingOpportunity thehousopp = new HousingOpportunity();
thehousopp.selectBuildingname = HousingResouce.Name;

and also remove ApexPages.currentPage().getParameters().put('selectBuildingname',HousingResouce.Name); from your class.


All Answers

Subhash GarhwalSubhash Garhwal
Hi,
In your test class you need to pass "selectBuildingname" string value Like :-
HousingOpportunity thehousopp = new HousingOpportunity();
thehousopp.selectBuildingname = HousingResouce.Name;

and also remove ApexPages.currentPage().getParameters().put('selectBuildingname',HousingResouce.Name); from your class.


This was selected as the best answer
ASFASF
Thanks Subhash,

            Its Working.....