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
Venkateswarlu PVenkateswarlu P 

Test Class for SOSL and Page Reference

public class DML_Account_DetailPage {    
    public string inputAccName		{set;get;}
    public string inputAccPhone		{set;get;}
    public string inputAccRating	{set;get;}	
    public string inputAccIndustry	{set;get;}
   
    public PageReference Create()
    {
        List<Account> accsList=[select id from Account where name=:inputAccName and phone=:inputAccPhone];
        if(accsList.size()>0)
        {
            ApexPages.message msg=new ApexPages.message(ApexPages.severity.ERROR,'Duplicate Record Found');
            ApexPages.addMessage(msg);
            return null;
        }
        else
        {
            Account acc=new Account();
            acc.name=inputAccName;
            acc.phone=inputAccPhone;
            acc.rating=inputAccRating;
            acc.Industry=inputAccIndustry;
            insert acc;
            PageReference p= new PageReference('/'+acc.id);
            return p;           
        }
    }
}
How to write test class for sosl and page reference.
 
Best Answer chosen by Venkateswarlu P
Raj VakatiRaj Vakati
@isTest

public class DML_Account_DetailPage_Test {
    
    public static testMethod void testMyController() {
        Account acc = new Account();
        acc.Name ='Test'; 
        acc.Phone='123123123' ;
        insert acc ; 
        
        PageReference pageRef = Page.Demo;
        Test.setCurrentPage(pageRef);
        DML_Account_DetailPage DMLcon = new DML_Account_DetailPage();
        DMLcon.inputAccName ='Test' ; 
        DMLcon.inputAccPhone ='123123123' ; 
        
        DMLcon.Create();
        
        
        DMLcon.inputAccName ='Test11' ; 
        DMLcon.inputAccPhone ='123123123111' ; 
        
        DMLcon.Create();
        
    }
}