title: Reset class static variable during unit test
body: <p>I am trying to write a unit test for a <em>legacy code</em>. The class which I'm testing has several static variables. My test case class has a few <code>@Test</code> methods. Hence all of them share the same state.</p>
  
  <p>Is there way to reset all static variables between tests?</p>
  
  <p>One solution I came up is to explicitly reset each field, e.g.:</p>
  
  <pre><code>field(MyUnit.class, "staticString").set(null, null);
  ((Map) field(MyUnit.class, "staticFinalHashMap").get(null)).clear();
  </code></pre>
  
  <p>As you see, each variable needs custom re-initialization. The approach is not easy to scale, there are a lot such classes in the legacy code base. Is there any way to reset everything at once? Maybe by reloading the class each time?</p>
  
  <p>As a possible good solution I think is to use something like powermock and create a separate classloader for each test. But I don't see easy way to do it.</p>