+ Start a Discussion
Umesh BP 38Umesh BP 38 

Controller test class?

How to write Test class for std controller and custom controller,wrapper class?
sfdcfoxsfdcfox
Create a new record, insert it, construct a Standard Controller, then test it:
@isTest class TestMyExt {
    @isTest static void test() {
        Account a = new Account(Name='Test');
        insert a;
        MyExt ext = new MyExt(new ApexPages.StandardController(a));
        ext.myMethod();
        System.assert(ext.someValue == someOtherValue);
    }
}
If the page is designed to handle new records, you can pass a new object directly to the standard controller:
MyExt ext = new MyExt(new ApexPages.StandardController(new Account());
More specific examples will need more specific questions, but this should get you started.
Veenesh VikramVeenesh Vikram
Hi,

Salesforce provides easy examples and descriptive documentation for testing out Controllers(Custom Controllers and Extension controllers).
You will find it helpful:https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm

If you are using a Standard Controller only, you wont need a test class as no extension is present.
Also for Wrapper class, you just need to call the constructor of wrapper class, and the same will be covered.

Hope this was helpful.

Veenesh