How to access Class members decorated using { get; set; }












2















I'm trying to save on boilerplate code so tried to use { get; set; } idiom, but the function will not resolve.



This is the class with decorated properties.



public class Counter {
private static Integer hits {get { if(hits == null) hits = 0; hits++; return hits; } private set; }
private static Boolean foo {get;set;}
}


My unit test, that will not build, I get the error: Method does not exist or incorrect signature: void getHits() from the type Counter on every assert line.



@isTest public class CounterTest extends AbstractTestCase {
@isTest static void testCounter() {
Test.startTest();
Counter count = new Counter();
assertNotNull(count);

// instance access, case sensitive and insensitive
System.assert(count.getHits() == 1);
System.assert(count.gethits() == 1);

// static access, case sensitive and insensitive
System.assert(Counter.getHits() == 2);
System.assert(Counter.gethits() == 2);

// even trivial example doesn't work
count.setFoo(true);
System.assert(getFoo());

// I even tried direct access,
System.debug('count : ' + Counter.hits);
System.assert(Counter.hits == 1);
Test.stopTest();
}
}


I've using v44 of apex, how on earth are these used?



Yes, there are lot of questions about { get; set; } but non address this specific point










share|improve this question























  • Related: salesforce.stackexchange.com/q/157006/660 - also as noted below, your class properties must be visible outside of the class itself (public, global, or @TestVisible).

    – Mark Pond
    34 mins ago
















2















I'm trying to save on boilerplate code so tried to use { get; set; } idiom, but the function will not resolve.



This is the class with decorated properties.



public class Counter {
private static Integer hits {get { if(hits == null) hits = 0; hits++; return hits; } private set; }
private static Boolean foo {get;set;}
}


My unit test, that will not build, I get the error: Method does not exist or incorrect signature: void getHits() from the type Counter on every assert line.



@isTest public class CounterTest extends AbstractTestCase {
@isTest static void testCounter() {
Test.startTest();
Counter count = new Counter();
assertNotNull(count);

// instance access, case sensitive and insensitive
System.assert(count.getHits() == 1);
System.assert(count.gethits() == 1);

// static access, case sensitive and insensitive
System.assert(Counter.getHits() == 2);
System.assert(Counter.gethits() == 2);

// even trivial example doesn't work
count.setFoo(true);
System.assert(getFoo());

// I even tried direct access,
System.debug('count : ' + Counter.hits);
System.assert(Counter.hits == 1);
Test.stopTest();
}
}


I've using v44 of apex, how on earth are these used?



Yes, there are lot of questions about { get; set; } but non address this specific point










share|improve this question























  • Related: salesforce.stackexchange.com/q/157006/660 - also as noted below, your class properties must be visible outside of the class itself (public, global, or @TestVisible).

    – Mark Pond
    34 mins ago














2












2








2








I'm trying to save on boilerplate code so tried to use { get; set; } idiom, but the function will not resolve.



This is the class with decorated properties.



public class Counter {
private static Integer hits {get { if(hits == null) hits = 0; hits++; return hits; } private set; }
private static Boolean foo {get;set;}
}


My unit test, that will not build, I get the error: Method does not exist or incorrect signature: void getHits() from the type Counter on every assert line.



@isTest public class CounterTest extends AbstractTestCase {
@isTest static void testCounter() {
Test.startTest();
Counter count = new Counter();
assertNotNull(count);

// instance access, case sensitive and insensitive
System.assert(count.getHits() == 1);
System.assert(count.gethits() == 1);

// static access, case sensitive and insensitive
System.assert(Counter.getHits() == 2);
System.assert(Counter.gethits() == 2);

// even trivial example doesn't work
count.setFoo(true);
System.assert(getFoo());

// I even tried direct access,
System.debug('count : ' + Counter.hits);
System.assert(Counter.hits == 1);
Test.stopTest();
}
}


I've using v44 of apex, how on earth are these used?



Yes, there are lot of questions about { get; set; } but non address this specific point










share|improve this question














I'm trying to save on boilerplate code so tried to use { get; set; } idiom, but the function will not resolve.



This is the class with decorated properties.



public class Counter {
private static Integer hits {get { if(hits == null) hits = 0; hits++; return hits; } private set; }
private static Boolean foo {get;set;}
}


My unit test, that will not build, I get the error: Method does not exist or incorrect signature: void getHits() from the type Counter on every assert line.



@isTest public class CounterTest extends AbstractTestCase {
@isTest static void testCounter() {
Test.startTest();
Counter count = new Counter();
assertNotNull(count);

// instance access, case sensitive and insensitive
System.assert(count.getHits() == 1);
System.assert(count.gethits() == 1);

// static access, case sensitive and insensitive
System.assert(Counter.getHits() == 2);
System.assert(Counter.gethits() == 2);

// even trivial example doesn't work
count.setFoo(true);
System.assert(getFoo());

// I even tried direct access,
System.debug('count : ' + Counter.hits);
System.assert(Counter.hits == 1);
Test.stopTest();
}
}


I've using v44 of apex, how on earth are these used?



Yes, there are lot of questions about { get; set; } but non address this specific point







apex class getter setter






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 6 hours ago









Martin of HessleMartin of Hessle

936




936













  • Related: salesforce.stackexchange.com/q/157006/660 - also as noted below, your class properties must be visible outside of the class itself (public, global, or @TestVisible).

    – Mark Pond
    34 mins ago



















  • Related: salesforce.stackexchange.com/q/157006/660 - also as noted below, your class properties must be visible outside of the class itself (public, global, or @TestVisible).

    – Mark Pond
    34 mins ago

















Related: salesforce.stackexchange.com/q/157006/660 - also as noted below, your class properties must be visible outside of the class itself (public, global, or @TestVisible).

– Mark Pond
34 mins ago





Related: salesforce.stackexchange.com/q/157006/660 - also as noted below, your class properties must be visible outside of the class itself (public, global, or @TestVisible).

– Mark Pond
34 mins ago










2 Answers
2






active

oldest

votes


















3














From Apex, you reference it the same as if it didn't have a getter or setter defined. The fact that it is private means you will need to use the @TestVisible annotation if you want to test against it, but also strongly indicates you shouldn't be testing it, or it shouldn't be private. Usually I will use a public getter and private setter.



public Integer myAttribute { get; private set; }


Once you do that, the reference is simply:



system.assertEquals(1, Counter.hits);


Please note that you should always prefer assertEquals(expected, observed) over assert(expected == observed), and that the expected value comes first. Following these rules will give you much clearer assertion failures, as would including a useful message (the optional third parameter).






share|improve this answer

































    1














    To clarify the syntax (if hits wasn't static):



    count.hits = 1; // calls the "set"
    Integer foo = count.hits; // calls the "get"


    You can't call count.getHits(). The calling syntax looks just like accessing a field.



    However, since "hits" is static, it is not tied to an instance of your class, but to your class directly. You would access it as Counter.hits.



    Then, you'll need to do something about visibility; either annotate with TestVisible, or make the property public.






    share|improve this answer










    New contributor




    Mike Lockett is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.




















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "459"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f247007%2fhow-to-access-class-members-decorated-using-get-set%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      From Apex, you reference it the same as if it didn't have a getter or setter defined. The fact that it is private means you will need to use the @TestVisible annotation if you want to test against it, but also strongly indicates you shouldn't be testing it, or it shouldn't be private. Usually I will use a public getter and private setter.



      public Integer myAttribute { get; private set; }


      Once you do that, the reference is simply:



      system.assertEquals(1, Counter.hits);


      Please note that you should always prefer assertEquals(expected, observed) over assert(expected == observed), and that the expected value comes first. Following these rules will give you much clearer assertion failures, as would including a useful message (the optional third parameter).






      share|improve this answer






























        3














        From Apex, you reference it the same as if it didn't have a getter or setter defined. The fact that it is private means you will need to use the @TestVisible annotation if you want to test against it, but also strongly indicates you shouldn't be testing it, or it shouldn't be private. Usually I will use a public getter and private setter.



        public Integer myAttribute { get; private set; }


        Once you do that, the reference is simply:



        system.assertEquals(1, Counter.hits);


        Please note that you should always prefer assertEquals(expected, observed) over assert(expected == observed), and that the expected value comes first. Following these rules will give you much clearer assertion failures, as would including a useful message (the optional third parameter).






        share|improve this answer




























          3












          3








          3







          From Apex, you reference it the same as if it didn't have a getter or setter defined. The fact that it is private means you will need to use the @TestVisible annotation if you want to test against it, but also strongly indicates you shouldn't be testing it, or it shouldn't be private. Usually I will use a public getter and private setter.



          public Integer myAttribute { get; private set; }


          Once you do that, the reference is simply:



          system.assertEquals(1, Counter.hits);


          Please note that you should always prefer assertEquals(expected, observed) over assert(expected == observed), and that the expected value comes first. Following these rules will give you much clearer assertion failures, as would including a useful message (the optional third parameter).






          share|improve this answer















          From Apex, you reference it the same as if it didn't have a getter or setter defined. The fact that it is private means you will need to use the @TestVisible annotation if you want to test against it, but also strongly indicates you shouldn't be testing it, or it shouldn't be private. Usually I will use a public getter and private setter.



          public Integer myAttribute { get; private set; }


          Once you do that, the reference is simply:



          system.assertEquals(1, Counter.hits);


          Please note that you should always prefer assertEquals(expected, observed) over assert(expected == observed), and that the expected value comes first. Following these rules will give you much clearer assertion failures, as would including a useful message (the optional third parameter).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 16 mins ago

























          answered 6 hours ago









          Adrian LarsonAdrian Larson

          106k19113240




          106k19113240

























              1














              To clarify the syntax (if hits wasn't static):



              count.hits = 1; // calls the "set"
              Integer foo = count.hits; // calls the "get"


              You can't call count.getHits(). The calling syntax looks just like accessing a field.



              However, since "hits" is static, it is not tied to an instance of your class, but to your class directly. You would access it as Counter.hits.



              Then, you'll need to do something about visibility; either annotate with TestVisible, or make the property public.






              share|improve this answer










              New contributor




              Mike Lockett is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.

























                1














                To clarify the syntax (if hits wasn't static):



                count.hits = 1; // calls the "set"
                Integer foo = count.hits; // calls the "get"


                You can't call count.getHits(). The calling syntax looks just like accessing a field.



                However, since "hits" is static, it is not tied to an instance of your class, but to your class directly. You would access it as Counter.hits.



                Then, you'll need to do something about visibility; either annotate with TestVisible, or make the property public.






                share|improve this answer










                New contributor




                Mike Lockett is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.























                  1












                  1








                  1







                  To clarify the syntax (if hits wasn't static):



                  count.hits = 1; // calls the "set"
                  Integer foo = count.hits; // calls the "get"


                  You can't call count.getHits(). The calling syntax looks just like accessing a field.



                  However, since "hits" is static, it is not tied to an instance of your class, but to your class directly. You would access it as Counter.hits.



                  Then, you'll need to do something about visibility; either annotate with TestVisible, or make the property public.






                  share|improve this answer










                  New contributor




                  Mike Lockett is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.










                  To clarify the syntax (if hits wasn't static):



                  count.hits = 1; // calls the "set"
                  Integer foo = count.hits; // calls the "get"


                  You can't call count.getHits(). The calling syntax looks just like accessing a field.



                  However, since "hits" is static, it is not tied to an instance of your class, but to your class directly. You would access it as Counter.hits.



                  Then, you'll need to do something about visibility; either annotate with TestVisible, or make the property public.







                  share|improve this answer










                  New contributor




                  Mike Lockett is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer








                  edited 42 mins ago









                  Mark Pond

                  18.2k13285




                  18.2k13285






                  New contributor




                  Mike Lockett is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 1 hour ago









                  Mike LockettMike Lockett

                  112




                  112




                  New contributor




                  Mike Lockett is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Mike Lockett is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Mike Lockett is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Salesforce Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f247007%2fhow-to-access-class-members-decorated-using-get-set%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Polycentropodidae

                      Magento 2 Error message: Invalid state change requested

                      Paulmy