Slaesforce FAQ

how to cover pagereference method in testclass in salesforce

by Claud Wehner 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 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 I create a test class for a custom 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.

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 you call a method in test class in Salesforce?

You can call the method from a test class, similar to how you call method from other classes. ClassName classInstanceObj = new ClassName(); classInstanceObj. MethodName();

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...

How do I debug a test class in Salesforce?

Go to Setup>Developer>Apex Test Execution>Select Tests> pick the testing class you want to see the debug logs from can click run.

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