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


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

public class Test_MyController {
 static testMethod void myUnitTest() {
       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();

You need to look at this documentation here:


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.