title: Mockito's spy does not work along with AspectJ using maven
body: <p>I have the following test class and I'm using Mockito's spy. By running my unit test using Eclipse (right click->Run as unit test) all tests pass which means that eclipse build process using m2e and AJDT works fine. </p>
  
  <pre class="lang-java prettyprint-override"><code>@RunWith(MockitoJUnitRunner.class)
  public class SampleTest {
  
  @Mock
  private AnotherClazz mockedClazz;
  
  @Spy
  @InjectMocks
  private SampleImpl sampleService = new SampleImpl() {
  
      @Override
      public void someMethod() {
         ...
      }
  };
  
  
  @Test
  public void someTest() throws Exception {
      sampleService.methodUnderTest();
  }
  </code></pre>
  
  <p>However when I use maven to run the tests I get the following exception.   </p>
  
  <pre class="lang-none prettyprint-override"><code>Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.183 sec &lt;&lt;&lt;     FAILURE!
      org.sample.SampleTest  Time elapsed: 0.182 sec  &lt;&lt;&lt; ERROR!
      org.mockito.exceptions.base.MockitoException: Problems initiating spied field     sampleService
      at     org.mockito.internal.runners.JUnit45AndHigherRunnerImpl$1.withBefores(JUnit45AndHigherRunnerImpl.java:27)
      at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:254)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
      at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
      at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
      at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
      at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
      at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
      at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
      at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
      at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
      at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
  Caused by: org.mockito.exceptions.base.MockitoException: 
  Mockito cannot mock this class: class org.sample.SamplelTest$1
  Mockito can only mock visible &amp; non-final classes.
  If you're not sure why you're getting this error, please report to the mailing list.
      ... 25 more
  Caused by: org.mockito.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException--&gt;null
      at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:238)
      at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378)
      at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318)
      at org.mockito.internal.creation.cglib.ClassImposterizer.createProxyClass(ClassImposterizer.java:123)
      at org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:57)
      at org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:49)
      at org.mockito.internal.creation.cglib.CglibMockMaker.createMock(CglibMockMaker.java:24)
      at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
      at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
      at org.mockito.Mockito.mock(Mockito.java:1285)
      at org.mockito.internal.configuration.injection.SpyOnInjectedFieldsHandler.processInjection(SpyOnInjectedFieldsHandler.java:43)
      at org.mockito.internal.configuration.injection.MockInjectionStrategy.process(MockInjectionStrategy.java:68)
      at org.mockito.internal.configuration.injection.MockInjectionStrategy.relayProcessToNextStrategy(MockInjectionStrategy.java:89)
      at org.mockito.internal.configuration.injection.MockInjectionStrategy.process(MockInjectionStrategy.java:71)
      at org.mockito.internal.configuration.injection.MockInjection$OngoingMockInjection.apply(MockInjection.java:93)
      at org.mockito.internal.configuration.DefaultInjectionEngine.injectMocksOnFields(DefaultInjectionEngine.java:20)
      at org.mockito.internal.configuration.InjectingAnnotationEngine.injectMocks(InjectingAnnotationEngine.java:100)
      at org.mockito.internal.configuration.InjectingAnnotationEngine.processInjectMocks(InjectingAnnotationEngine.java:62)
      at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:56)
      at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:108)
      ... 25 more
  Caused by: java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.mockito.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:385)
      at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:220)
      ... 44 more
  Caused by: java.lang.VerifyError: Cannot inherit from final class
      at java.lang.ClassLoader.defineClass1(Native Method)
      at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
      ... 50 more
  </code></pre>
  
  <p>What is here the difference between the AJC compiler used in Eclipse with AJDT  and my AspectJ maven plugin configuration? Which configuration or phase am I missing here?</p>
  
  <p>Here is my pom.xml:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;dependency&gt;
          &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
          &lt;artifactId&gt;aspectjrt&lt;/artifactId&gt;
          &lt;version&gt;1.6.11&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
              &lt;artifactId&gt;aspectj-maven-plugin&lt;/artifactId&gt;
              &lt;version&gt;1.4&lt;/version&gt;
              &lt;configuration&gt;
                  &lt;source&gt;1.6&lt;/source&gt;
                  &lt;target&gt;1.6&lt;/target&gt;
              &lt;/configuration&gt;
              &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;phase&gt;process-sources&lt;/phase&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;compile&lt;/goal&gt;
                          &lt;goal&gt;test-compile&lt;/goal&gt;
                      &lt;/goals&gt;
                  &lt;/execution&gt;
              &lt;/executions&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p>Update: I haven't found a solution yet but I know why is this happening. The problem actually lies here on overriding the someMethod(). Removing the method override or commenting out the aspectj maven plugin solves the problem as a workaround. I cannot let AspectJ out of my project so I have to mock the someMethod() as well without overriding it (e.g. using Mockito itself). It looks like a bug for me but I'm not quite sure does it come from AspectJ or Mockito.</p>