title: When testing a method, can you mock variables on singletons which that method refers to?
body: <p>I've got a method that I'd like to test with JUnit/Mockito.</p>
  
  <p>The method takes in one Integer, <code>numberOfRowsThatTheUserWants</code>.</p>
  
  <p>The method then compares that value, against another value <code>totalNumberOfRowsInDB</code> and will return the requested number of rows(randomly picked). If the user requests more than is available, they'll just get the total that are available.</p>
  
  <p><em>All is fine up to this point.</em></p>
  
  <p>The problem I have, is testing this methods behaviour.</p>
  
  <p><code>totalNumberOfRowsInDB</code> is a variable held on a singleton. My reason for doing this was because it would be set at startup and would not change during the execution of the application, and I wanted to avoid constant unneccssay calls to the DB to constantly count the rows when it would never change.</p>
  
  <p>Testing works if I make my tests assume there is a total of 10 rows (as that is what I have on the actual base), however I don't want my tests to be dependent on my scrub data, because if I add a few more rows, it'll break the tests.</p>
  
  <p><strong>Question : How can I modify(mock) a static variable that resides INSIDE of the method I'm actually trying to test?</strong> I'm completely open to suggestions / advice on how to improve my design if its awfully wrong...</p>
  
  <p>This is a little demo to show a simplified version of my issue:</p>
  
  <pre><code>public class MockMe
  {
      public static void main(String[] args)
      {
          // Setting to 5, but this comes from user input
          Integer numberOfRowsThatTheUserWants = 5;
          MockMe myMockMe = new MockMe();
          myMockMe.myMethod(numberOfRowsThatTheUserWants);
      }
  
      private void myMethod(Integer numberOfRowsThatTheUserWants)
      {
          if(numberOfRowsThatTheUserWants &gt; MySingleton.getInstance().getTotalRowsOnDB())
          {
              System.out.println("You've requested more than is available, so I'll just give you all I've got instead");
          }
          else
          {
              System.out.println("Here are your " + numberOfRowsThatTheUserWants + " rows as requested, sir!");
          }
      }
  }
  
  class MySingleton
  {
      private static MySingleton mySingleton;
      private int totalRowsOnDB;
  
      private MySingleton()
      {
          //private constructor to prevent instantiation
      }
  
      public int getTotalRowsOnDB()
      {
          return totalRowsOnDB;
      }
  
      public static MySingleton getInstance()
      {
          if (mySingleton == null)
          {
              mySingleton = new MySingleton();
              // this is actually set by SELECT COUNT(*) FROM TABLENAME but setting to 10 for purposes of demo
              //DBManager dbManager = new DBManagerImpl();
              //mySingleton.totalRowsOnDB = dbManager.getTotalRows();
              mySingleton.totalRowsOnDB = 10;
          }
          return mySingleton;
      }
  }
  </code></pre>