title: Replacing PowerMock's @PrepareForTest programmatically?
body: <p>I am using PowerMock to mock static methods in junit tests, typically done as follows:</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>@RunWith(PowerMockRunner.class)
  @PrepareForTest({Foo.class,Bar.class})
  public class SomeUnitTest {
  
    @Before
    public void setUpTest() {
      setUpFoo();
      setUpBar();
    }
  
    private void setUpFoo() {
      mockStatic(Foo.class);
      when(Foo.someStaticMethod()).thenReturn(1);
    }
  
    private void setUpBar() {
      mockStatic(Bar.class);
      when(Bar.someStaticMethod()).thenReturn(2);
    }
  
    @Test
    public void someTestCase() {
      ...
    }
  
  }
  </code></pre>
  
  <p>This works fine, but I'm finding that specifying the <code>@PrepareForTest</code> annotation is preventing me from making my testing API flexible. </p>
  
  <p>What I'd like to do is something like the following:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class MockLibraryOne {
  
    public static void setUpLibraryOne() {
      setUpFoo();
      setUpBar();
    }
  
    private static void setUpFoo() {
      mockStatic(Foo.class);
      when(Foo.someStaticMethod()).thenReturn(1);
    }
  
    private static void setUpBar() {
      mockStatic(Bar.class);
      when(Bar.someStaticMethod()).thenReturn(2);
    }
  
  }
  
  @RunWith(PowerMockRunner.class)
  public class SomeUnitTest {
  
    @Before
    public void setUpTest() {
      MockLibraryOne.setUpLibraryOne();
    }
  
    @Test
    public void someTestCase() {
      ...
    }
  
  }
  </code></pre>
  
  <p>Here my unit test has a dependency on <code>LibraryOne</code>, but it does not know which classes <code>LibraryOne</code> depends on, so it does not know which classes to add to the <code>@PrepareForTest</code> annotation.</p>
  
  <p>I could make <code>SomeUnitTest</code> extend <code>MockLibraryOne</code> and add the <code>@PrepareForTest</code> annotation to the <code>MockLibraryOne</code> class, but I will have dependencies on more than just <code>MockLibraryOne</code> in other unit tests, so inheritance is not a general solution.</p>
  
  <p>Is there some way of programmatically preparing a class for testing under PowerMock, instead of using the <code>@PrepareForTest</code> annotation? For example, something like the following:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class MockLibraryOne {
  
    public static void setUpLibraryOne() {
      setUpFoo();
      setUpBar();
    }
  
    private static void setUpFoo() {
      prepareForTest(Foo.class);
      mockStatic(Foo.class);
      when(Foo.someStaticMethod()).thenReturn(1);
    }
  
    private static void setUpBar() {
      prepareForTest(Bar.class);
      mockStatic(Bar.class);
      when(Bar.someStaticMethod()).thenReturn(2);
    }
  
  }
  </code></pre>
  
  <p>I guess it would be nice if <code>PowerMockRunner</code> processed the <code>@PrepareForTest</code> annotation a little differently: for each specified class, it should not only add that class (and its hierarchy) to the list of classes to prepare for mocking, but then examine that class to see if it has any <code>@PrepareForTest</code> annotations as well:</p>
  
  <pre class="lang-java prettyprint-override"><code>@RunWith(PowerMockRunner.class)
  @PrepareForTest({MockLibraryOne.class})
  public class SomeUnitTest {
    ...
  }
  
  @PrepareForTest({Foo.class,Bar.class})
  public class MockLibraryOne {
    ...
  }
  </code></pre>
  
  <p>}</p>
  
  <p>So in this the <code>@PrepareForTest</code> annotation on <code>SomeUnitTest</code> would find <code>MockLibraryOne</code>, and the <code>@PrepareForTest</code> annotation there would drag in <code>Foo.class</code> and <code>Bar.class</code> as well.</p>
  
  <p>So perhaps writing my own test runner to replace <code>PowerMockRunner</code> may be a solution.</p>
  
  <p>Or perhaps there's a simpler solution, using <code>PowerMockAgent</code> class, for example?</p>
  
  <p>edit: Mock Policies may be one solution: <a href="https://code.google.com/p/powermock/wiki/MockPolicies" rel="noreferrer">https://code.google.com/p/powermock/wiki/MockPolicies</a></p>
  
  <p>edit: Mock Policies works with <code>PowerMockRunner</code> but not (it seems) with <code>PowerMockRule</code> (which I sometimes require due to class loader issues).</p>