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
AlanLamAlanLam 

Very Easy Question - For all that know :)

Dear all,

 

I am in the process of trying to write a test method for a VF controller. From what I have understood, I have to incorporate the test code in the actual controller. When I try I keep getting bracket errors etc. 

 

Can anyone send me an example of how the code of controller with a test looks?

 

Many thanks

 

Alan

bob_buzzardbob_buzzard

You don't have to keep the test code within the controller.  I must admit I tend to do that so that I can get at private properties, but there's no reason why you can't have a separate test class.

 

If you keep the test code with the controller code you want something like the following:

 

public with sharing MyController
{
   // controller methods

   /////////////////////////
   //
   // Unit Tests
   //
   /////////////////////////

   private static testmethod void TestController()
   {
      MyController ctrl=new MyController();
      // tests and asserts here
   }

}

 

AlanLamAlanLam

Thanks for the prompt reply Bob.

 

Will give it a go.

 

PS. Always impressed with your ability to reply to so many posts haha!!