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
torotoro 

Test problem

Hello,

I'm trying to create test method for my control class.

Here is my control class;

 

 

public class VFController { private final OpportunityLineItem o; public VFController(ApexPages.StandardController stdController) { this.o = (OpportunityLineItem)stdController.getRecord(); } public string theId; // Code we will invoke on page load. public PageReference autoRun() { theId = ApexPages.currentPage().getParameters().get('id'); OpportunityLineItem newprod; string oppID; string prodID; if (theId == null) { // Display the Visualforce page's content if no Id is passed over return null; } upsert(o); for (OpportunityLineItem o:[select Buffers__c, ATC__c, CNC__c, CTS_PSI__c, Chip_Conveyor__c, CreatedById, CreatedDate, ServiceDate, IsDeleted, Head_Change__c, Head_Type__c, OpportunityId, L_U__c, LastModifiedById, LastModifiedDate, Description, Id, ListPrice, Number_of_Pallets__c, Other_Options__c, PricebookEntryId, Product_Family__c, Quantity, RGV__c, Ship_Date__c, SortOrder, Spindle__c, Spindle_Taper__c, SystemModstamp, Table__c, TotalPrice, Delivery_Date__c from OpportunityLineItem where id =:theId]) { // Do all the dirty work we need the code to do upsert(o); newprod = o.clone(false); newprod.Machine_SN__c = 'TBD'; oppID = newprod.OpportunityId; insert newprod; } // Redirect the user back to the original page PageReference pageRef = new PageReference('/' + newprod.id + '/e?retURL='+ oppID); pageRef.setRedirect(true); return pageRef; } 

      

and here is my test method:

 

      static testMethod void testAutoRun(){ OpportunityLineItem opl = new OpportunityLineItem(); opl = [select Id, OpportunityId, Spindle__c, Quantity, Table__c, Ship_Date__c, Delivery_Date__c, Machine_SN__c,TotalPrice from OpportunityLineItem limit 1]; pagereference pageref = new pagereference('/'+opl.Id); Test.setcurrentpage(pageref); VFController vfc = new VFController(new ApexPages.StandardController(opl)); vfc.theId = opl.Id; vfc.autorun(); } 

 

 

How can I put value on theId? The test always go with theId ==null and never gets lower half of the code because I can't put value in theId....

 

This is my first testmethod.

Does anyone can help me? 

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
colemabcolemab

You might look into some code like this:

 

String key = 'name';
String value = 'Caroline';
ApexPages.currentPage().setParameters().put(key, value);

Note

The setParameters()method is only valid inside test methods.

 

 

More info at this link.

All Answers

colemabcolemab

I would suggest that if you want help, you make your code a bit more readable by adding line breaks and tabs instead of just posting one big block of text.  Just saying.

torotoro

 

 

I should have review before post ....here is the nice looking code. hopefully somebody can help.

 

public class VFController { 
	private final OpportunityLineItem o; 
	public VFController(ApexPages.StandardController stdController) { 

 	this.o = (OpportunityLineItem)stdController.getRecord(); 
	} 

	public string theId; 
	
	// Code we will invoke on page load. 

	public PageReference autoRun() { 
	
	theId = ApexPages.currentPage().getParameters().get('id'); 
	OpportunityLineItem newprod; 
	string oppID; string prodID; 
	if (theId == null) { 
	// Display the Visualforce page's content if no Id is passed over return null; 
	}
	for (OpportunityLineItem o:[select erviceDate, OpportunityId, Description, Id, ListPrice, PricebookEntryId, Quantity, Ship_Date__c, TotalPrice, Delivery_Date__c from OpportunityLineItem where id =:theId]) { 
	upsert(o); 
	newprod = o.clone(false); 
	oppID = newprod.OpportunityId; 
	insert newprod; 
	} 
	// Redirect the user back to the original page 

	PageReference pageRef = new PageReference('/' + newprod.id + '/e?retURL='+ oppID); 
	pageRef.setRedirect(true); 
	return pageRef; 
} 

 

 

 Here is my test code

 

     static testMethod void testAutoRun(){ 
	
	OpportunityLineItem opl = new OpportunityLineItem(); 
	opl = [select Id, OpportunityId, Quantity, Ship_Date__c, Delivery_Date__c, TotalPrice from OpportunityLineItem limit 1]; 
	
	pagereference pageref = new pagereference('/'+opl.Id); 
	Test.setcurrentpage(pageref); 
	VFController vfc = new VFController(new ApexPages.StandardController(opl)); 
	vfc.theId = opl.Id; 
	vfc.autorun(); 
} 

 Thank you

colemabcolemab

You might look into some code like this:

 

String key = 'name';
String value = 'Caroline';
ApexPages.currentPage().setParameters().put(key, value);

Note

The setParameters()method is only valid inside test methods.

 

 

More info at this link.

This was selected as the best answer
torotoro

 

 

  Thank you.

 

  This is the code I ended up and it covers 100%.

 

static testMethod void testAutoRun(){        
	OpportunityLineItem opl = [select Id, OpportunityId, Spindle__c, Quantity, Table__c, 
	Ship_Date__c, Delivery_Date__c, Machine_SN__c,TotalPrice from OpportunityLineItem limit 1];      
	VFcontroller vfc = new VFController(new ApexPages.StandardController(opl));      
	
	vfc.autorun();            

	pagereference pageref = page.ProdDetailEdit;      
	pageref.getparameters().put('Id',opl.Id);      
	Test.setcurrentpage(pageref);      
	vfc.newprod = opl.clone(false);            
	vfc.autorun();      
	System.assertEquals(opl.opportunityId, vfc.newprod.OpportunityId);      
}