title: How to mock a method in an ENUM class?
body: <p>I am working on writing JUNIT test case for my below ENUm class. My below class will only give me the hostname for the current machine where I am running my code. While I am writing JUNIT test, how can I mock the below class, so that I can change <code>getHostName()</code> method whenever I want to so that whenever I am calling <code>getDatacenter()</code>, it can return me whatever hostname I am passing by mocking it. I don't want to make it as a parametrized.</p>
  
  <p>I just want to test certain cases while changing the hostname while mocking it.</p>
  
  <pre><code>public enum DatacenterEnum {
      DEV, DC1, DC2, DC3;
  
  
      public static String forCode(int code) {
      return (code &gt;= 0 &amp;&amp; code &lt; values().length) ? values()[code].name() : null;
      }
      private static final String getHostName() {
          try {
              return InetAddress.getLocalHost().getCanonicalHostName().toLowerCase();
          } catch (UnknownHostException e) {
              s_logger.logError("error = ", e);
          }
  
          return null;
      }
  
      public static String getDatacenter() {
          return getHostName();
      }
  }
  </code></pre>