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

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

Test Class : Now we write test class for Controller extension

  1. @istest.
  2. public class yourExtController_TC {
  3. static testMethod void test1()
  4. {
  5. Account acc = new Account(name=’testAccount’);
  6. insert acc;
  7. Test. startTest();
  8. ApexPages. StandardController account = new ApexPages. StandardController(acc);

What is standard controller in Apex?

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.

What is the difference between standard and custom controllers?

The Custom Controller, is one that is used to inherit the Standard Controller’s functionality, whilst allowing to get access to extra information, like: Access data that is not available with a Standard Controller, like more than one level down child or related objects. Create custom behavior (properties or methods)

What is a standard set controller?

StandardSetController objects allow you to create list controllers similar to, or as extensions of, the pre-built Visualforce list controllers provided by Salesforce. The StandardSetController class also contains a prototype object. This is a single sObject contained within the Visualforce StandardSetController class.

When to use controller extension in apex test?

When writing unit tests for controller extension and custom controller classes, you can set query parameters that can then be used in the tests. A controller extension is an Apex class that extends the functionality of a standard or custom controller.

How are unit tests used in apex class?

Unit tests are class methods that verify whether a particular piece of code is working correctly as expected. Apex unit test methods take no arguments, commit no data to the database, and are decorated with the @isTest annotation in the method definition. public AccountControllerExtension (ApexPages.StandardController controller) {

How to create apex Test class for VF pages?

The class constructor takes a single argument of type ApexPages.StandardController or C ustomControllerName, where CustomControllerName is the name of a custom controller you want to extend. Controller extensions like all Apex codes should be covered by unit tests.

How to write test class for standard controller along with?

It will solve your problem. When we use StandardController no need to use ApexPages.currentPage ().getParameters ().get (‘Id’) instead just use controller.getId (); Because in your test, you do not set that parameter before calling the constructor.