title: Declaring Jmockit mock parameters on @BeforeMethod of TestNG
body: <p>I've been testing my code behavior using <code>TestNG</code> and <code>JMockit</code> for a while now and I have had no specific issue with their combination. Today I came across a situation where I needed to  mock one of my internal dependencies, in the so called, <em>type wide manner</em> and I did not need to keep that mock around since none of the test cases dealt with it directly while they counted on the mocked version functionality. So, naturally, I put the mocking logic in my <code>@BeforeMethod</code>. Here is a sample:</p>
  
  <pre><code>public class SampleTest
  {
      @Mocked
      @Cascading
      private InnerDependency dependency;
  
      @BeforeMethod
      public void beforeMethod()
      {
          new NonStrictExpectations()
          {
              {
                  dependency.getOutputStream((String)any);
                  result = new Delegate&lt;OutputStream&gt;()
                  {
                      public OutputStream getOutputStream(String url)
                      {
                          return null;
                      }
                  };
              }
          };
      }
  
      @Test
      public void testNormalOperation()
      {
          // The test whose desired behavior depends on dependency being mocked out
          // ..
      }
  }
  </code></pre>
  
  <p>But, since my tests do not care about the mocked <code>dependency</code> explicitly, I'm not willing to declare it as a test class field, unlike what is done above. To my knowledge of <code>JMockit</code> The only options remaining would be:</p>
  
  <ol>
  <li><p>Declare <code>dependency</code> as a local mock field:</p>
  
  <pre><code>new NonStrictExpectations()
  {
      @Cascading
      private InnerDependency dependency;
      {
          //...
      }
  }
  </code></pre></li>
  <li><p>Declare <code>dependency</code> as an input argument for <code>beforeMethod()</code>, similar to what is done for normal <code>@Test</code> methods:</p>
  
  <pre><code>@BeforeMethod
  public void beforeMethod(@Mocked @Cascading final InnerDependency dependency)
  {
     // ...
  }
  </code></pre></li>
  </ol>
  
  <p>I see that <strong>JMockit 1.6+</strong>  would not like the first option and warns with <code>WARNING: Local mock field "dependency" should be moved to the test class or converted to a parameter of the test method</code>. Hence, to keep everyone happy, I'm ruling this option out.</p>
  
  <p>But for the second option, <code>TestNG</code> (currently 6.8.6) throws exception when running the test saying <code>java.lang.IllegalArgumentException: wrong number of arguments</code>. I don't see this behavior with normal <code>@Test</code> cases passed with <code>@Mocked</code> parameters. Even playing with <code>@Parameter</code> and <code>@Optional</code> will not help (and should not have!).</p>
  
  <p>So, is there any way I could make this work without declaring the <em>unneccessary test class mock field</em>, or am I missing something here?</p>
  
  <p>Thanks</p>