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
Cristian TrifCristian Trif 

How can I test my VisualForce page ?

Hello guys, I have the following Visualforce page, name is screen1VSF with a controller.

<apex:page controller="screen1">
   <apex:form >
       <apex:pageBlock >
           <apex:pageBlockSection >
               <apex:inputField value="{!account.name}"/>
               <apex:commandButton value="Save" action="{!save}"/>
           </apex:pageBlockSection>
       </apex:pageBlock>
   </apex:form>
</apex:page>

public class screen1 
{
    public Account myAccount;
    public Account callAccMethod; 
    
    public Account getAccount()
    {
        if(myAccount == null)
        {
            myAccount = new account();
        }
        return myAccount;
    }

    public PageReference save()
    {
       callAccMethod = getAccount();
       if(callAccMethod != null)
       {
           insert myAccount;
       }
       
       PageReference pageRef = new PageReference('/apex/screen2');
       pageRef.getParameters().put('id', myAccount.Id);
       return pageRef;
     }
}

I read some documentation from here: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm but I'm still confused how this testing with VF works. Any ideas how can I test my visualforce page and explain how you did it, i want to learn.
Saoni PaitandiSaoni Paitandi
Hi 
VF page with Controller means you have write tesst class for your controller.
Cristian TrifCristian Trif
Hi Saoni, i already did that.

@isTest
public class screen1Test {
    public static testMethod void testController()
    {
        PageReference pageRef = Page.screen1VSF;
        Test.setCurrentPage(pageRef);

        screen1 sc = new screen1();
        String nextPage = sc.save().getUrl();        
    }
}
I'm stuck here. I don't know what to test and how can i test my controller. Any ideas, i need to understand how this works.
Deepak Kumar SharmaDeepak Kumar Sharma
Just Try The Below Code:-
@isTest
private class ScreenTest {
    static testMethod void UnitTest1(){
        screen1 scr = new screen1();
        Account acc = scr.getAccount();
        acc.name = 'TestAcc';
        scr.save();
    }

}

Happy Coding!! :)
Saoni PaitandiSaoni Paitandi
Hi Christin!!
Try as below:
@isTest 
public class ControllerTestClass 
{
 static testMethod void testMethod1() 
 {
 Account testAccount = new Account();
 testAccount.Name='Test Account' ;
 insert testAccount;

 Test.StartTest(); 

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

  screen1 testAccPlan = new screen1();
  
  //testAccPlan.save(); call all your function here
 Test.StopTest();
 }
 
Cristian TrifCristian Trif

I have 100% code coverage! 

Nice. So what you wrote in this small code was only to create new object screen1() then you created a variable acc with data type account and this variable calls  the getAccount() method and then you created an account with name 'TestAcc' and then call the save() method, right ? Can you explain me please why i have 100% code coverage?

Saoni PaitandiSaoni Paitandi
Cristian the main idea behind test class is to create test data so that you can test all the methods present in the controller .So here we are testing the page reference method that we have in controller and creating test data.
You can refer the below refernces:http://amitsalesforce.blogspot.in/2017/02/salesforce-tutorial-how-to-learn-test.html
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html
Please mark it as best answer which if it resolved your query
Cristian TrifCristian Trif
@isTest 
public class ControllerTestClass 
{
   static testMethod void testMethod1() 
   {
        screen1 testAccPlan = new screen1();
        Account testAccount = testAccPlan.getAccount();
        testAccPlan.save();

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

        System.assertEquals(expected, actual);
          //screen1 testAccPlan = new screen1();
       
       
     }
}

I have 100% percent with this code. I covered all methods. Now i need to make assertions. How can i make some assertions on this test method ?
Deepak Kumar SharmaDeepak Kumar Sharma
Hi Cristian,
This Error is occured because you haven't inserted any account with the required field 'name' in it. You don't need to create an instance of account record at #line 7 since your test environment already return it  after calling getAccount() method. so just provide name for account after getting the instance returned by test method and then call save method with same instance that you have already created at #line 6 i.e sc.save() please do not create the new instance again for the class at #line 13 So apart from :-
         screen1 sc = new screen1();
         Account testAccount = new Account();
         testAccount.Name='TestAccount' ;
        Account acc = sc.getAccount();
Just Write :-
      
Test.StartTest();
        screen1 sc = new screen1();
        Account acc = scr.getAccount();
        acc.name = 'TestAcc';
        sc.save();
       Test.StopTest();
    }
If this really help you please mark it as best answere. :)