JMockit mock private method in @PostConstruct
<h2>Context</h2>

<p>In my class under test there is a <code>@PostConstruct</code> annotated method that invokes another private method.</p>

<pre><code>@PostConstruct
public void init() {
    longWork(); // private method
}
</code></pre>

<p>JMockit's default behaviour is to execute <code>@PostConstruct</code> methods of <code>@Tested</code> classes upon injection.</p>

<blockquote>
  <p>If a @Tested class has a method annotated with
  javax.annotations.PostConstruct, it should be executed right after
  injection.</p>
</blockquote>

<p><a href="https://github.com/jmockit/jmockit1/issues/13" rel="nofollow">https://github.com/jmockit/jmockit1/issues/13</a></p>

<h2>Problem</h2>

<p>JMockit calls the <code>init()</code> method, what I don't want.</p>

<p>From the thread dump:</p>

<pre><code>at com.me.Worker.longWork(Worker.java:56)
at com.me.Worker.longWork.init(Worker.java:47)
...
at mockit.internal.util.MethodReflection.invoke(MethodReflection.java:96)
</code></pre>

<p>How can that call be mocked/removed/blocked?</p>

<h2>Attempts</h2>

<p>I tried to mock the <code>init</code> and <code>longWork</code> methods as shown below. However that results in <code>NullPointerException</code> as <code>sut</code> is not injected yet.</p>

<pre><code>@Before
public void recordExpectationsForPostConstruct()
{
    new NonStrictExpectations(sut) {{ invoke(sut, "init"); }};
}
</code></pre>