-6
<apex:page controller="myController" tabStyle="Account">
<apex:form>
<apex:pageBlock title="Congratulations {!$User.FirstName}">
You belong to Account Name: <apex:inputField value="{!account.name}"/>
<apex:commandButton action="{!save}" value="save"/>
</apex:pageBlock>
</apex:form>
</apex:page>

controller:

public class MyController {
private final Account account;
public MyController() {
account = [SELECT Id, Name, Site FROM Account
WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
}
public Account getAccount() {
return account;
}
public PageReference save() {
update account;
return null;
}
}

How to write test class for this custom controller? can anybody help me?i am the newer of the controller

2
@isTest
public class Test_MyController {
 static testMethod void myUnitTest() {
        test.startTest();
       Account acc = new Account();
         acc.Name = ' test';
  acc.Site = '';
       insert acc;
  PageReference testPage = new pagereference('/apex/VisualforcePage');
       ApexPages.currentPage().getParameters().put( 'id', acc.id );

 MyController MyControllerObj = new MyController();
  PageReference pageref = MyControllerObj.save();
        MyControllerObj.getAccount();
        test.stopTest();
}
}
2

You need to look at this documentation here:

http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm

But in short you will need to:

  1. Create a test class and method.
  2. Insert into the database all the supporting data you expect the controller to retrieve.
  3. Set the current page to be the VF page that the controller backs.
  4. Instantiate an instance of the controller.
  5. Call the pageLoad method to fire your logic.
  6. Check the properties of the controller to ensure they contain the values you were expecting.
  • thanks but i need this conttoller test class plese help me – Deepika Jul 7 '15 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.