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
Rahul Sharma 390Rahul Sharma 390 

to call a pageref method into test class

my class ------>

public without sharing class AllBeatMaps {
   
    public list<Beat_Map__c> bmapList {get;set;}
    public list<User> salesRepList {get;set;}
    public String pageTitle {get;set;}
    public String querytype {get;set;}
   
    public AllBeatMaps(){
     pageTitle='SELECT BEAT MAP';
     String salesRep = 'Sales Rep';
     querytype = apexpages.currentPage().getParameters().get('type');
     String query1 = 'SELECT Id,Name,Beat_Definition__c FROM Beat_Map__c';
     String query2 = 'SELECT Id,Name,Userrole.Name FROM User WHERE UserRole.Name=';
     
     if(querytype!=null && querytype.trim().length()>0){
      if(querytype=='beatmap'){
       bmapList = Database.query(query1);
      }
      if(querytype=='salesrep'){
       pageTitle = 'SELECT SALES REP';
       query2+='\''+salesRep+'\'';
       salesRepList=Database.query(query2); 
      }
     }
    }
   
}

Test Class ---------->

@isTest
public class AllBeatMapsTest {
    public static testmethod void allbeatmptest(){
        profile SalesRep = [Select Id FROM Profile Where Name= 'Sales Representative'];
        userRole urp = new UserRole(Name = 'Sales Rep');
        insert urp;
        User ur = new User(UserRoleid=urp.Id,ProfileId = SalesRep.Id,Alias = 'hfsre',Country='India',Email='defo1@urturiurain.com',EmailEncodingKey='UTF-8', LastName='oiuriuterh', LanguageLocaleKey='en_US',LocaleSidKey='en_US',TimeZoneSidKey='America/Los_Angeles', UserName='wdjhfgb@b.com');
        insert ur;
 
        system.runAs(ur){
        list<User> usa = new list<User>();
        usa.add(ur);
           
            Beat_Map__c bm = new Beat_Map__c();
            bm.Beat_Definition__c = 'Gurugram Sector 14,17,21,23';
            insert bm;
            list<Beat_Map__c> bmc = new list<Beat_Map__c>();
            bmc.add(bm);
  PageReference pageRef = Page.AllBeatMaps;
  Beat_Map__c bcc = new Beat_Map__c();
  bcc.Beat_Definition__c = 'Gurugram Sector 45,17,21,23';
  insert bcc;
  
   Test.setCurrentPage(pageRef);
   pageRef.getParameters().put('id',bcc.id);
           
            AllBeatMaps obh = new AllBeatMaps();
           
 }
}
           
          
         
           
        }
   
 
Best Answer chosen by Rahul Sharma 390
Gokula KrishnanGokula Krishnan
Hi Rahul,

Try this,
 
@isTest
public class AllBeatMapsTest {
    public static testmethod void allbeatmptest(){
        profile SalesRep = [Select Id FROM Profile Where Name= 'Sales Representative'];
        userRole urp = new UserRole(Name = 'Sales Rep');
        insert urp;
        
		User ur = new User(UserRoleid=urp.Id,ProfileId = SalesRep.Id,Alias = 'hfsre',Country='India',Email='defo1@urturiurain.com',EmailEncodingKey='UTF-8', LastName='oiuriuterh', LanguageLocaleKey='en_US',LocaleSidKey='en_US',TimeZoneSidKey='America/Los_Angeles', UserName='wdjhfgb@b.com');
        insert ur;
 
        system.runAs(ur){
			list<User> usa = new list<User>();
			usa.add(ur);
           
            Beat_Map__c bm = new Beat_Map__c();
            bm.Beat_Definition__c = 'Gurugram Sector 14,17,21,23';
            insert bm;
			
			ApexPages.currentPage().getParameters().put('querytype', 'beatmap');
			AllBeatMaps ains = new AllBeatMaps();
			
			ApexPages.currentPage().getParameters().put('querytype', 'salesrep');
			AllBeatMaps ains = new AllBeatMaps();
         }  
	}
}
Thanks  !!!

If it helps you, please mark is as best answer, so it will be helpful for other developers.
 

All Answers

Gokula KrishnanGokula Krishnan
Hi Rahul,

Try this,
 
@isTest
public class AllBeatMapsTest {
    public static testmethod void allbeatmptest(){
        profile SalesRep = [Select Id FROM Profile Where Name= 'Sales Representative'];
        userRole urp = new UserRole(Name = 'Sales Rep');
        insert urp;
        
		User ur = new User(UserRoleid=urp.Id,ProfileId = SalesRep.Id,Alias = 'hfsre',Country='India',Email='defo1@urturiurain.com',EmailEncodingKey='UTF-8', LastName='oiuriuterh', LanguageLocaleKey='en_US',LocaleSidKey='en_US',TimeZoneSidKey='America/Los_Angeles', UserName='wdjhfgb@b.com');
        insert ur;
 
        system.runAs(ur){
			list<User> usa = new list<User>();
			usa.add(ur);
           
            Beat_Map__c bm = new Beat_Map__c();
            bm.Beat_Definition__c = 'Gurugram Sector 14,17,21,23';
            insert bm;
			
			ApexPages.currentPage().getParameters().put('querytype', 'beatmap');
			AllBeatMaps ains = new AllBeatMaps();
			
			ApexPages.currentPage().getParameters().put('querytype', 'salesrep');
			AllBeatMaps ains = new AllBeatMaps();
         }  
	}
}
Thanks  !!!

If it helps you, please mark is as best answer, so it will be helpful for other developers.
 
This was selected as the best answer
Rahul Sharma 390Rahul Sharma 390
Thnx 
Gokula Krishnan