Below is the Apex Controller and Test Class which i have written for it.
public with sharing class Condosearchcontroller {
public list <Condo__c> Cond {get;set;}
public String LeadRecordId {get;set;}
public string beds {get;set;}
public string squarefootage {get;set;}
public String SFminimum {get;set;}
public String SFmaximum {get;set;}
public string InitialListPrice {get;set;}
public String ILPminimum {get;set;}
public String ILPmaximum {get;set;}
public string Unitnamevf {get;set;}
public Boolean refreshPage {get; set;}
public Condosearchcontroller() {
}
public Condosearchcontroller(ApexPages.StandardController controller) {
}
public List<SelectOption> getbedsOptions() {
List<SelectOption> bedsoptions = new List<SelectOption>();
bedsoptions.add(new SelectOption('','ALL'));
bedsoptions.add(new SelectOption('0','0'));
bedsoptions.add(new SelectOption('1','1'));
bedsoptions.add(new SelectOption('2','2'));
bedsoptions.add(new SelectOption('3','3'));
return bedsoptions;
}
public PageReference Search(){
String SFmin = SFminimum.remove(',');
String SFmax = SFmaximum.remove(',');
String ILPmin = ILPminimum.remove(',');
String ILPmax = ILPmaximum.remove(',');
string searchquery= 'select name,Beds__c,Square_Footage__c,Initial_List_Price__c,Status__c from Condo__c where name!=null and Status__c in (\'Available\',\'Unsold\') ';
if (beds == 'ALL'){
searchquery+= ' and Beds__c = 0,1,2,3 ';
}
else
if (beds >= '0'){
searchquery+= ' and Beds__c = '+beds+'';
}
else
if (SFminimum > '0' && SFmaximum >'0') {
searchquery+= ' and Square_Footage__c >= '+SFmin+' and Square_Footage__c <= '+SFmax+' ';
}
else
if (ILPminimum > '0' && ILPmaximum >'0') {
searchquery += 'and Initial_List_Price__c >= '+ILPmin+' and Initial_List_Price__c <= '+ILPmax+' ';
}
if (beds >= '0' && SFminimum >'0' && SFmaximum > '0'){
searchquery+= ' and Beds__c = '+beds+' and Square_Footage__c >= '+SFmin+' and Square_Footage__c <= '+SFmax+' ';
}
else
if (beds >= '0' && ILPminimum > '0' && ILPmaximum >'0'){
searchquery += 'and Beds__c = '+beds+' and Initial_List_Price__c >= '+ILPmin+' and Initial_List_Price__c <= '+ILPmax+' ';
}
if(beds >= '0' && SFminimum >'0' && SFmaximum >'0' && ILPminimum >'0' && ILPmaximum >'0'){
searchquery += ' and Beds__c = '+beds+' and Square_Footage__c >= '+SFmin+' and Square_Footage__c <= '+SFmax+' and Initial_List_Price__c >= '+ILPmin+' and Initial_List_Price__c <= '+ILPmax+' ';
}
else
if(beds >= '0' && SFminimum >'0') {
searchquery += ' and Beds__c = '+beds+' and Square_Footage__c >= '+SFmin+' ';
}
else
if(beds >= '0' && SFmaximum > '0') {
searchquery += ' and Beds__c = '+beds+' and Square_Footage__c <= '+SFmax+' ';
}
else
if(beds >= '0' && ILPminimum > '0') {
searchquery += ' and Beds__c = '+beds+' and Initial_List_Price__c >= '+ILPmin+' ';
}
else
if(beds >= '0' && ILPmaximum > '0') {
searchquery += ' and Beds__c = '+beds+' and Initial_List_Price__c <= '+ILPmax+' ';
}
else
if(SFminimum > '0') {
searchquery += ' and Square_Footage__c >= '+SFmin+' ';
}
else
if(SFmaximum >'0') {
searchquery += ' and Square_Footage__c <= '+SFmax+' ';
}
else
if(ILPminimum > '0')
{
searchquery += ' and Initial_List_Price__c >= '+ILPmin+'';
}
else
if(ILPmaximum > '0') {
searchquery += ' and Initial_List_Price__c <= '+ILPmax+' ';
}
if ( SFmin != null && SFmin.isAlpha() || SFmax != null && SFmax.isAlpha() || ILPmin != null && ILPmin.isAlpha() || ILpmax != null && ILPmax.isAlpha()){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Numerical values for SquareFootage and Price Fields'));
}
else
cond = Database.query(searchquery);
return null;
}
Public Pagereference addtolead() {
Id UnitId;
Unitnamevf=System.currentPageReference().getParameters().get('UnitNameParam');
LeadRecordId = ApexPages.CurrentPage().getparameters().get('id');
List<Lead> Lds = new List<Lead>([select Id,Unit__c from Lead where Id= :LeadRecordId]);
List<Condo__c> Cnd = New List<Condo__c>([select Id,Name from Condo__c where Name= :Unitnamevf]);
if(!Cnd.isEmpty()){
UnitId = Cnd[0].Id;
Lds[0].Unit__c = UnitId;
update Lds[0];
}
refreshPage=true;
return null;
}
public void Reset(){
beds = null;
SFminimum = null;
SFmaximum = null;
ILPminimum = null;
ILPmaximum = null;
if(Cond != null) {
Cond.clear();
}
}
}
when i run the below test class
@isTest
Public Class TestCondosearchcontroller {
static Testmethod void Bedstestmethod(){
String SFmin;
String SFmax;
String ILPmin;
String ILPmax;
//create a CIM Asset
CIM_Assets__c myAsset = new CIM_Assets__c(Name='condolistname1',Property_Type__c='Condo');
insert myAsset;
//create some condo's and add to the asset
Condo__c Cnd1 = new Condo__c(CIM_Assets__c=myAsset.Id,Beds__c=1,Initial_List_Price__c=1200000,Status__c='UnSold',Square_Footage__c = 1000);
Condo__c Cnd2 = new Condo__c(CIM_Assets__c=myAsset.Id,Beds__c=0,Initial_List_Price__c=1500000,Status__c='Available',Square_Footage__c = 1200);
Condo__c Cnd3 = new Condo__c(CIM_Assets__c=myAsset.Id,Beds__c=2,Initial_List_Price__c=1300000,Status__c='UnSold',Square_Footage__c = 1300);
Condo__c Cnd4 = new Condo__c(CIM_Assets__c=myAsset.Id,Beds__c=3,Initial_List_Price__c=1400000,Status__c='Sold',Square_Footage__c = 1400);
Condo__c Cnd5 = new Condo__c(CIM_Assets__c=myAsset.Id,Beds__c=1,Initial_List_Price__c=1345000,Status__c='Sold',Square_Footage__c = 850);
Condo__c Cnd6 = new Condo__c(CIM_Assets__c=myAsset.Id,Beds__c=1,Initial_List_Price__c=1450000,Status__c='Available',Square_Footage__c = 1350);
Condo__c[] allCondos = new Condo__c[]{Cnd1,Cnd2,Cnd3,Cnd4,Cnd5,Cnd6};
insert allCondos;
PageReference pageRef = Page.CondosSearchPage;
pageRef.getParameters().put('beds', 'ALL');
pageRef.getParameters().put('SFmin', '500');
pageRef.getParameters().put('SFmax', '1500');
pageRef.getParameters().put('ILPmin', '1,000,000');
pageRef.getParameters().put('ILPmax', '1,500,000');
Test.setCurrentPageReference(pageRef);
Condosearchcontroller cndser =new Condosearchcontroller();
cndser.Search();
}
static Testmethod void getBedsOptions(){
Test.StartTest();
Condosearchcontroller cndser = new Condosearchcontroller();
cndser.getbedsOptions();
Test.StopTest();
}
static Testmethod void AddtoLead(){
string Unitnamevf = 'R2-202';
Id UnitId;
Boolean Refreshpage;
CIM_Assets__c CIM = New CIM_Assets__c();
CIM.name = 'Test CIM';
CIM.Property_Type__c = 'Condo';
insert CIM;
Condo__c Cnd2 = New Condo__c();
//Cnd2.name = 'R1-10111';
Cnd2.CIM_Assets__c = CIM.Id;
insert Cnd2;
Lead ld = New Lead();
ld.LastName = 'Test Lead';
ld.Company = 'Test Company';
insert ld;
Test.StartTest();
if(Cnd2 != null){
UnitId = Cnd2.Id;
ld.Unit__c = UnitId;
Update ld;
}
Condosearchcontroller cndser = new Condosearchcontroller();
cndser.addtolead();
Test.StopTest();
}
static Testmethod void Reset(){
String beds = '3';
String SFmin = '500';
String SFmax = '1000';
String ILPmin = '1,000,000';
String ILPmax = '1,500,000';
test.starttest();
Condosearchcontroller cndser = new Condosearchcontroller();
cndser.Reset();
Test.StopTest();
}
}
I am constantly getting a error message saying TestCondosearchcontroller Bedstestmethod : Fail System.NullPointerException: Attempt to de-reference a null object Class.Condosearchcontroller.Search: line 40, column 1
Class.TestCondosearchcontroller.Bedstestmethod: line 59, column 1