Mock final class with Mockito 2
<p>I'm removing Powermock from the project I'm currently working on, so I'm trying to rewrite some existing unitary test only with Mockito (mockito-core-2.2.28).</p>
<p>When I run the test, I have the following error:</p>
<blockquote>
<p>org.mockito.exceptions.base.MockitoException:</p>
<p>Cannot mock/spy class com.ExternalpackagePath.Externalclass</p>
<p>Mockito cannot mock/spy because :</p>
<ul>
<li>final class</li>
</ul>
</blockquote>
<p>I know that this question has already been asked (<a href="https://stackoverflow.com/questions/14292863/how-to-mock-a-final-class-with-mockito">How to mock a final class with mockito</a>, <a href="https://stackoverflow.com/questions/17083432/mock-objects-calling-final-classes-static-methods-with-mockito">Mock objects calling final classes static methods with Mockito</a>), but I didn't find the answer I'm looking for.</p>
<p>Here is an extract of my code :</p>
<pre><code>public class MyClassToTest extends TestCase {
    private MyClass myClass;
    @Mock private Externalclass ext;  // This class is final, I would like to mock it

    @Override
    protected void setUp() throws Exception {
        MockitoAnnotations.initMocks(this); // &lt;&lt;&lt;&lt; The exception is thrown here
        ext = Mockito.mock(Externalclass.class);
    }
}
</code></pre>
<p>As mentioned in the Mockito documentation (<a href="https://github.com/mockito/mockito/wiki/What%27s-new-in-Mockito-2" rel="noreferrer">https://github.com/mockito/mockito/wiki/What%27s-new-in-Mockito-2</a>, §Mock the unmockable), I added the org.mockito.plugins.MockMaker file. This is the tree of my project :</p>
<ul>
<li>project
<ul>
<li>src
<ul>
<li>com.packagePath.myPackage
<ul>
<li>myClass</li>
</ul>
</li>
</ul>
</li>
<li>test
<ul>
<li>com.packagePath.myPackage
<ul>
<li>myClassToTest</li>
</ul>
</li>
<li>resources
<ul>
<li>mockito-extensions
<ul>
<li>org.mockito.plugins.MockMaker</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>I also tries to put the &quot;resources&quot; directory in &quot;src&quot;, in a subdir called &quot;test&quot;, but the result is still the same.</p>
<p>I thought that mocking a final was possible with Mockito v2. Does someone have an idea of what is missing here ?</p>
<p>Thanks!</p>