Slaesforce FAQ

how to cover pagereference in test class in salesforce

by Otilia Schowalter Published 2 years ago Updated 2 years ago
image

Please let me know how to cover below line in apex test class: PageReference pageRef = new PageReference ('/'+acc.ID); pageRef.setRedirect (true); return pageRef; Below is the test code I wrote: Test.setCurrentPageReference (new PageReference ('/'+acc.Id)); System.currentPageReference ().getParameters ().put ('id', acc.id);

Full Answer

How do I use PageReference in Salesforce?

PageReference returns a reference to a Visualforce page, including its query string parameters. Using the page reference, use the getParameters method to return a map of the specified query string parameter names and values. Then a call to the get method specifying id returns the value of the id parameter itself.

How do I create a test class for a controller in Salesforce?

How to cover pagereference method in test class for Standard Controller:-First create record. Account acc = New Account(); acc.Name = 'Test Account'; INSERT acc;Page reference to your VF Page. ... Pass necessary parameter. ... Pass your object to controller. ... Call controller. ... Call pageRef mymethod. ... Put system asserts.

How do you write a test class for a standard set controller in Salesforce?

Sample Code: Account a = new Account( Name = 'Test' ); Class_Name obj = new Class_Name( new ApexPages. StandardController( a ) ); here the controller is referred to a page where the standard controller is Account.

How do you cover a wrapper list in test class?

You can simply call the wrapper class with methods in the test class to increase the code coverage. It will cover your Wrapper class and methods. Hope this explanation will resolve your query.

How do I find the test class code coverage in Salesforce?

Follow these steps every time you run the code coverage to have reliable coverage details:Navigate to Setup.In the Quick Find Search type 'Apex' and select 'Apex Test Execution'Click Options.Deslect 'Store Only Aggregated Code Coverage' and click 'OK'Click 'View test history'Click 'Clear all test history'More items...

What is Apexpages StandardController?

StandardController objects reference the pre-built Visualforce controllers provided by Salesforce. The only time it is necessary to refer to a StandardController object is when defining an extension for a standard controller. StandardController is the data type of the single argument in the extension class constructor.

Do we need test class for lightning component?

No need to write test classes for lightning components.In salesforce we need to write test classes for apex classes and triggers to test your business logic and to ensure that your Apex classes and triggers work as expected.

How do I test Visualforce pages?

This is a specific locator generated by Provar which is anchored to the APEX page definition. We recommend using this option where available. To map a Visualforce field, right-click on your target field and select 'Add to Test Case'. Then click Add or Add & Do to save the Test Step.

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9