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
hoomelhoomel 

URL parameter in test class

Hello,

 

i wrote pretty basic data collecting class in Apex for generating a PDF document.

This is the class:

 

public with sharing class pdfData {
	public String sys = Apexpages.currentPage().getParameters().get('sys');
	public String userID = UserInfo.getUserId();
	public Integer lineCount = 0;
	public Integer maxLines = 21;
	public List<ItemSystemRelation__c> list1 = new List<ItemSystemRelation__c>();
	public List<ItemSystemRelation__c> list2 = new List<ItemSystemRelation__c>();
	public User curUser = [Select Street, PostalCode, City FROM User WHERE Id = :userID];
	public Rental__c rental = [Select Id, Name,Period__c, Owner.Name, Rental_fee__c, Calculation_starts_week__c, Start__c, End__c, Account_ERP_No__c,Account_Tax_number__c, Account_City__c, Account_Street__c, Account_Zip_Code__c, Account__r.Name FROM Rental__c WHERE Rental_System__c = :sys limit 1];
	public Rental_Systems__c rsystem = [Select Id, Name, System_Name__c FROM Rental_Systems__c WHERE Id = :sys limit 1];
	private List<ItemSystemRelation__c> items = [Select Item__r.Short_description__c, Item__r.Name, Item__r.Item_Name__c FROM ItemSystemRelation__c WHERE Rental_System__c = :sys AND Item__r.Active__c = true order by Item__r.Name];
	public String curDate = system.today().format();
	public pdfData(){
	}
	public Rental__c getRental(){
		return rental;
	}
	public Rental_Systems__c getRSystem(){
		return rsystem;
	}
	public String getToday(){
		return System.today().format();
	}
	public String getStart(){
		return rental.Start__c.format();
	}
	public String getEnd(){
		return rental.End__c.format();
	}
	public User getUser(){
		return curUser;
	}
	public Integer getItemCount(){
		return items.size();
	}
	public List<ItemSystemRelation__c> getItems(){
		return items;
	}
	public String getCurDate(){
		return curDate;
	}
	public void populateLists(){
		Integer totalLines = 0;
		for(Integer i = 0;i < items.size();i++){
			if(items.get(i).Item__r.Short_description__c != null){
				totalLines = math.round(totalLines + items.get(i).Item__r.Short_description__c.length()/80);
			}else{
				totalLines++;
			}
			if(totalLines < maxLines){
				System.debug(items.get(i));
				list1.add(items.get(i));
			}else{
				list2.add(items.get(i));
			}
		}
	}
	public List<ItemSystemRelation__c> getList1(){
		populateLists();
		return list1;
	}
	public Integer getSize1(){
		return list1.size();
	}
	public List<ItemSystemRelation__c> getList2(){
		return list2;
	}
	public Boolean getShowList2(){
		if(list2.size() < 1){return false;}else{return true;}
	}

 

 

I am now writing the test methods to deploy it in production, but it fails at the database query where the URL parameter is used.

How can I set this variable when creating a new instance of the class in the beginning of my test method?

Or is there any way around this problem?

 

Regards,

hoomel

 

Best Answer chosen by Admin (Salesforce Developers) 
TehNrdTehNrd

Can you post the test class? This is typically how URL params are set in a test:

 

 

Test.setCurrentPageReference(new PageReference('Page.myPage')); 
System.currentPageReference().getParameters().put('id', idVariable);

 

 

All Answers

gm_sfdc_powerdegm_sfdc_powerde

Since this class is not a controller, why do you want to read URL parameter here? It would be much easier if you read the URL parameter in your controller and pass it as an argument to your constructor.  Advantages - better separation of concerns, better testability.

TehNrdTehNrd

Can you post the test class? This is typically how URL params are set in a test:

 

 

Test.setCurrentPageReference(new PageReference('Page.myPage')); 
System.currentPageReference().getParameters().put('id', idVariable);

 

 

This was selected as the best answer
hoomelhoomel

@gm_sfdc_powerde: why do you think it is not a controller? I use it as a controller for a page generating a PDF document.

This page is acccessed via a button in an objects related list, hence I need the URL parameter.

 

@TehNrd: Thanks for your reply, i had the second line of your code, but after creating the new instance. Thinking about your suggestion, i realized that it needed to be before that.

The complete test is now working correctly!

 

Regards,

hoomel