Clearing or resetting a singleton in test class

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I'm writing a test method to reset the value of a singleton variable, however I have no clue how to do it. please can someone assist me with the test code to test the following:



public class Service extends CompositeService 

.....
//SINGLETON
private static Service singleton;

public static Service get()
if (singleton == null)
singleton = new Service ();

return singleton;


private Service ()
....
...









share|improve this question



























    up vote
    1
    down vote

    favorite












    I'm writing a test method to reset the value of a singleton variable, however I have no clue how to do it. please can someone assist me with the test code to test the following:



    public class Service extends CompositeService 

    .....
    //SINGLETON
    private static Service singleton;

    public static Service get()
    if (singleton == null)
    singleton = new Service ();

    return singleton;


    private Service ()
    ....
    ...









    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm writing a test method to reset the value of a singleton variable, however I have no clue how to do it. please can someone assist me with the test code to test the following:



      public class Service extends CompositeService 

      .....
      //SINGLETON
      private static Service singleton;

      public static Service get()
      if (singleton == null)
      singleton = new Service ();

      return singleton;


      private Service ()
      ....
      ...









      share|improve this question













      I'm writing a test method to reset the value of a singleton variable, however I have no clue how to do it. please can someone assist me with the test code to test the following:



      public class Service extends CompositeService 

      .....
      //SINGLETON
      private static Service singleton;

      public static Service get()
      if (singleton == null)
      singleton = new Service ();

      return singleton;


      private Service ()
      ....
      ...






      apex unit-test code-coverage






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 40 mins ago









      Minz

      182




      182




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote













          If your test class needs to "interfere" with private variables that are not accessible through your class's public API, you should add the @TestVisible annotation:



          @TestVisible
          private static Service singleton;


          Your unit test can then do



          Service.singleton = null;


          when needed.



          It's broadly preferable, where possible, to test using the public API, as this insulates your test class from the implementation details of your class and makes it run tests like any other consumer of the class's API. However, there are lots of situations, as it sounds like you've discovered, where it does become necessary to reach into the private state of a class during a test.






          share|improve this answer




















            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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f237775%2fclearing-or-resetting-a-singleton-in-test-class%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote













            If your test class needs to "interfere" with private variables that are not accessible through your class's public API, you should add the @TestVisible annotation:



            @TestVisible
            private static Service singleton;


            Your unit test can then do



            Service.singleton = null;


            when needed.



            It's broadly preferable, where possible, to test using the public API, as this insulates your test class from the implementation details of your class and makes it run tests like any other consumer of the class's API. However, there are lots of situations, as it sounds like you've discovered, where it does become necessary to reach into the private state of a class during a test.






            share|improve this answer
























              up vote
              3
              down vote













              If your test class needs to "interfere" with private variables that are not accessible through your class's public API, you should add the @TestVisible annotation:



              @TestVisible
              private static Service singleton;


              Your unit test can then do



              Service.singleton = null;


              when needed.



              It's broadly preferable, where possible, to test using the public API, as this insulates your test class from the implementation details of your class and makes it run tests like any other consumer of the class's API. However, there are lots of situations, as it sounds like you've discovered, where it does become necessary to reach into the private state of a class during a test.






              share|improve this answer






















                up vote
                3
                down vote










                up vote
                3
                down vote









                If your test class needs to "interfere" with private variables that are not accessible through your class's public API, you should add the @TestVisible annotation:



                @TestVisible
                private static Service singleton;


                Your unit test can then do



                Service.singleton = null;


                when needed.



                It's broadly preferable, where possible, to test using the public API, as this insulates your test class from the implementation details of your class and makes it run tests like any other consumer of the class's API. However, there are lots of situations, as it sounds like you've discovered, where it does become necessary to reach into the private state of a class during a test.






                share|improve this answer












                If your test class needs to "interfere" with private variables that are not accessible through your class's public API, you should add the @TestVisible annotation:



                @TestVisible
                private static Service singleton;


                Your unit test can then do



                Service.singleton = null;


                when needed.



                It's broadly preferable, where possible, to test using the public API, as this insulates your test class from the implementation details of your class and makes it run tests like any other consumer of the class's API. However, there are lots of situations, as it sounds like you've discovered, where it does become necessary to reach into the private state of a class during a test.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 35 mins ago









                David Reed

                22.9k41642




                22.9k41642



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f237775%2fclearing-or-resetting-a-singleton-in-test-class%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    List of Gilmore Girls characters

                    What does second last employer means? [closed]

                    One-line joke