+ Start a Discussion
Kishan Kumar 77Kishan 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;
    }
}
ANUTEJANUTEJ (Salesforce Developers) 
Hi Kishan,

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
Maharajan CMaharajan C
Please use the below code and you will get 95% coverage:

 
@isTest
public class OppSearchTest {
    Static testmethod void testOppSearchbyAmount(){
        
        Account acc = new Account(Name = 'Test Account');
        insert acc;
            
        Opportunity opp = new Opportunity(Name ='Test Opp',StageName='Prospecting',Amount=10000,CloseDate = system.today() + 60);
        insert opp;
        
        OppSearch os = new OppSearch();
        os.searchText = String.valueOf(opp.Amount);
        
        Test.startTest();
        	os.searchOpportunity();
        Test.stopTest();
    }
    
    Static testmethod void testOppSearchbyName(){
        
        Account acc = new Account(Name = 'Test Account');
        insert acc;
            
        Opportunity opp = new Opportunity(Name ='Test Opp',StageName='Prospecting',Amount=10000,CloseDate = system.today() + 60);
        insert opp;
        
        OppSearch os = new OppSearch();
        os.searchText = acc.Name;
        
        Test.startTest();
        	os.searchOpportunity();
        Test.stopTest();
    }
    
    Static testmethod void testOppSearchNegative(){
        OppSearch os = new OppSearch();
        os.searchText = '';
        os.searchOpportunity();
        
        OppSearch os1 = new OppSearch();
        os1.searchText = 'Opp Search';
        os1.searchOpportunity();
        
        OppSearch os2 = new OppSearch();
        os2.searchText = '5000';
        os2.searchOpportunity();
    }
}

Thanks,
Maharajan.C
Kishan Kumar 77Kishan Kumar 77
Thank you so much Maharajan!!