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
Vivek Kumar 257Vivek Kumar 257 

how to cover code of Standard controller in test class?

Amit Chaudhary 8Amit Chaudhary 8

I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm


You write a test class for this the same way that you would any other:
- Set up some data for the Apex Class to access
- Execute a method/methods:-
- Verify the behaviour with asserts.

Sample code for you
@isTest 
public class ExtensionTestClass 
{
 static testMethod void testMethod1() 
 {
 Account testAccount = new Account();
 testAccount.Name='Test Account' ;
 insert testAccount;

 Test.StartTest(); 
  ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
  myControllerExtension testAccPlan = new myControllerExtension(sc);

  PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
  pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
  Test.setCurrentPage(pageRef);

  //testAccPlan.save(); call all your function here
 Test.StopTest();
 }
}

 
Narender Singh(Nads)Narender Singh(Nads)
Hi Vivek,
For standard controllers you don't need to write test classes. Test classes for standard controllers are already present in the metadata repository of salesforce.

Thanks.
Vivek Kumar 257Vivek Kumar 257
i want to cover code which is written in this method public LinkController(ApexPages.StandardController controller) { }
Amit Chaudhary 8Amit Chaudhary 8
Post your code so that we can help you or you can try writting code with above example
Narender Singh(Nads)Narender Singh(Nads)
Hi vivek,
You can do something like this
Test.starttest();
ApexPages.StandardController sc = new ApexPages.StandardController(PASS_YOUR_SOBJECT_HERE);
YOUR_CLASS_NAME Obj= new YOUR_CLASS_NAME (sc);
Test.stoptest();
Let me know if it helps
Thanks!