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
Bob BaileyBob Bailey 

Building a test with setters

Trying to find a way around my previous testing problem I added this method to the test class:

static testMethod void testContributionSearchControllerDirect () {
      //instantiate a page     
     PageReference pg = Page.Contributions_Search_v9;
     // instantiate the controller
        ContributionsSearchController_v9 controller = new ContributionsSearchController_v9();
    // Plug in variable values
       controller.setcontributorFname('a');
       controller.setcontributorLname('z');
       controller.setMOC_Lname('g');
       controller.setsoqlLimitPage('limit 20');

 

The controller has this code between the class defn and the constructor:

   Public string contributorFname{
       get {return contributorFname; }
       set;
       }
   Public string contributorLname{
       get {return contributorLname; }
       set;
       }
   Public string MOC_Lname{
       get {return MOC_Lname; }
       set;
       }
   Public string soqlLimitPage{
       get {return soqlLimitPage; }
       set;
       } 


I get an error when I try to setContributorFname:
Error: Compile Error: Method does not exist or incorrect signature: [ContributionsSearchController_v9].setcontributorFname(String) at line 12 column 8. This is the line:

       controller.setcontributorFname('a');

 

I get the same error with the other lines if I comment out the first offender.

 

I'm baffled by this, I think I'm using the setter properly but clearly something is wrong. Thanks for any help.

 

....Bob

Best Answer chosen by Admin (Salesforce Developers) 
S91084S91084

Hi,

 

Instead of using setMethod try the following:

 

controller.contributorFname= 'a';

controller.contributorLname = 'z';

controller.MOC_Lname= 'g';

controller.soqlLimitPage = 'limit 20';

 

Hope this works for you!

All Answers

S91084S91084

Hi,

 

Instead of using setMethod try the following:

 

controller.contributorFname= 'a';

controller.contributorLname = 'z';

controller.MOC_Lname= 'g';

controller.soqlLimitPage = 'limit 20';

 

Hope this works for you!

This was selected as the best answer
Bob BaileyBob Bailey

Works great! THANK YOU!

 

But why...

S91084S91084

when you declare a variabl e as follows:

 

public Account a {get;set;}

 

In the above example you are generating getters and setters method. You have just declared the variable that will get the value from you page and set it.

 

You method works when you define in the following way,

 

private Account a;

 

public Account geta(){

    return a;

}

public Account seta(Account a ){

 

     this.a = a;

}