title: Initialize mocks via @Mock before @Rule
body: <p>I have a test class which is testing a Dropwizard resource and using the JUnit test rule <code>ResourceTestRule</code>. It sets up many mocks and I want to replace this idiom:</p>
  
  <p><code>Foo foo = mock(Foo.class);</code></p>
  
  <p>which is a bit verbose with this</p>
  
  <p><code>@Mock Foo foo;</code></p>
  
  <p>However the Dropwizard <code>ResourceTestRule</code> needs to reference these mocks like so</p>
  
  <pre><code>@Rule
  public ResourceTestRule resources = ResourceTestRule.builder()
          .addResource(new BarResource(foo))
          .build();
  </code></pre>
  
  <p>My problem is that the <code>@Rule</code> runs before <code>@Mock</code> initializes the mock, and so <code>foo</code> is null when <code>BarResource</code> is instantiated, and I then I get null pointer exceptions when my tests run.</p>
  
  <p>Here's a minimal example without Dropwizard stuff to demonstrate the issue:</p>
  
  <pre><code>import org.junit.Rule;
  import org.junit.Test;
  import org.junit.rules.TestRule;
  import org.junit.runner.Description;
  import org.junit.runner.RunWith;
  import org.junit.runners.model.Statement;
  import org.mockito.Mock;
  import org.mockito.runners.MockitoJUnitRunner;
  
  @RunWith(MockitoJUnitRunner.class)
  public class TestTest {
      @Mock String foo;
  
      @Rule
      public TestRule testRule = new TestRule() {
          {
              assert foo != null; // &lt;&lt;&lt; this assertions fails
          }
  
          @Override
          public Statement apply(Statement statement, Description description) {
              return statement;
          }
      };
  
      @Test
      public void test() {
  
      }
  }
  </code></pre>
  
  <p>If I could control the ordering between <code>@Mock</code> and <code>@Rule</code> I could avoid this problem. Or perhaps there's an alternative I'm not thinking of.</p>
  
  <p>Any advice appreciated, thanks!</p>