You need to sign in to do that
Don't have an account?
Kishan Kumar 77
I have a custom controller that is used for searching Opportunity. i need help to write the test class for it. Thanks in advance.
public with sharing class OppSearch {
public List<Opportunity> OppList { get; set; }
public String searchText { get; set; }
public Id selectedRec {get;set;}
public OppSearch()
{
OppList = new List<Opportunity>();
}
public PageReference searchOpportunity()
{
OppList = new List<Opportunity>();
if(String.isBlank(searchText))
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter some value'));
}
if(String.isNotBlank(searchText))
{
if(searchText.isNumeric())
{
Decimal searchstringDouble = decimal.valueOf(searchText);
OppList = [select Id,Name,Account.Name,StageName,Type,Amount,CloseDate
from Opportunity
where Amount=:searchstringDouble ];
if(OppList.size()==0)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Search failed as criteria do not match'));
}
}
else {
String newSearchText = searchText+'%';
OppList =[select Id,Name,Account.Name,StageName,Type,Amount,CloseDate
from Opportunity
where Name like:newSearchText or Account.Name like:newSearchText
or StageName like:newSearchText or Type like:newSearchText];
if(OppList.size()==0)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Search failed as criteria do not match'));
}
}
}
return null;
}
}
public List<Opportunity> OppList { get; set; }
public String searchText { get; set; }
public Id selectedRec {get;set;}
public OppSearch()
{
OppList = new List<Opportunity>();
}
public PageReference searchOpportunity()
{
OppList = new List<Opportunity>();
if(String.isBlank(searchText))
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter some value'));
}
if(String.isNotBlank(searchText))
{
if(searchText.isNumeric())
{
Decimal searchstringDouble = decimal.valueOf(searchText);
OppList = [select Id,Name,Account.Name,StageName,Type,Amount,CloseDate
from Opportunity
where Amount=:searchstringDouble ];
if(OppList.size()==0)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Search failed as criteria do not match'));
}
}
else {
String newSearchText = searchText+'%';
OppList =[select Id,Name,Account.Name,StageName,Type,Amount,CloseDate
from Opportunity
where Name like:newSearchText or Account.Name like:newSearchText
or StageName like:newSearchText or Type like:newSearchText];
if(OppList.size()==0)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Search failed as criteria do not match'));
}
}
}
return null;
}
}
You could do something like this:
>> Create an instance of the class.
>> Call the function that is searching the opportunities.
>> Check if you are getting the appropriate reference with system.assert statements.
>> Check the code coverage to see if you can improve the coverage anywhere.
I hope this helps and in case if this comes handy can you please choose this as best answer so that it can be used by others in the future.
Also, you can take reference of https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm documentation.
Regards,
Anutej
Thanks,
Maharajan.C