Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Mockito's spy does not work along with AspectJ using maven

Hi Sina.

I have answered about a week ago on StackOverflow, maybe you have not noticed:
http://stackoverflow.com/a/30828293/1082681

Regards
-- 
Alexander Kriegisch
http://scrum-master.de


Sina schrieb am 01.06.2015 20:13:

Hi Andy,
> 
> 
> In fact my SampleImpl class is a stateless EJB which cannot be final and I’ve used AspectJ previously with EJBs without any problem as they were not marked as final classes. There is one point unclear for me: Even if the ordering of compilation varies from one environment to another one the result should be the same, meaning that nobody should mark the class as final or an extended type of that class as final class in implementation of the proxies. I looked at the decompiled code of my test class and it extends the SampleImpl class and marks it as final when the aspectj is also in play but when it is commented out in my pom.xml the class is not marked as final anymore.
> 
> Something goes wrong probably on Mockito’s side that it marks the extended class as final. 
> 
> 
> I’ve also posted the same message in Mockito’s mailing list. I hope to get an answer from there. 
> 
> Thank you for the response and declaration. 
> 
> Cheers,
> 
> Sina
> 
>  
> 
>> 
>> On Jun 1, 2015, at 4:33 PM, Andy Clement <andrew.clement@xxxxxxxxx <mailto:andrew.clement@xxxxxxxxx> > wrote:
>> 
>> 
>> My initial thought is that it is an ordering thing. AspectJ uses a pipeline system where types at different stages through the pipeline are represented in different ways (if they haven’t made it into the weaver yet they have an eclipse representation, once through the weaver they have a bcel representation). The ordering of compilation is not enforced other than aspects go first down the pipeline.  This means compiling in different environments (on different JDK levels, doing a build inside eclipse, outside eclipse, etc) can lead to all the other types flowing down the pipeline in the different orders.
>> 
>> 
>> This sounds bad:
>> 
>> 
>>>> 
>>>> 
>>>> Caused by: java.lang.VerifyError: Cannot inherit from final class
>>>> 
>>>> 
>>>> 
>> But if AspectJ were making that mistake I’d perhaps expect to see the issue without mockito in the mix.
>> 
>> 
>> cheers,
>> 
>> Andy
>> 
>> 
>>> On May 31, 2015, at 8:42 AM, Sina <sina.golesorkhi@xxxxxxxxx <mailto:sina.golesorkhi@xxxxxxxxx> > wrote:
>>> 
>>> 
>>> 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.
>>> 
>>> 
>>>> On May 30, 2015, at 10:45 PM, Sina <sina.golesorkhi@xxxxxxxxx <mailto:sina.golesorkhi@xxxxxxxxx> > wrote:
>>>> 
>>>> 
>>>> Hi there I have using the AspectJ along with Mockito’s spy I have the  following problem that I have posted on stackoverflow: 
>>>> 
>>>> 
>>>> http://stackoverflow.com/questions/30551184/mockitos-spy-does-not-work-along-with-aspectj-using-maven <http://stackoverflow.com/questions/30551184/mockitos-spy-does-not-work-along-with-aspectj-using-maven> 
>>>> 
>>>> 
>>>> 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. 
>>>>     @RunWith(MockitoJUnitRunner.class)
>>>>     public class SamplelTest {
>>>> 
>>>> 
>>>>     @Spy
>>>>     @InjectMocks
>>>>     private SamplelImpl sampleService = new SamplelImpl() {
>>>> 
>>>>         @Override
>>>>         public void someMethod() {
>>>>            ...
>>>>         }
>>>>     };
>>>> 
>>>> 
>>>>     @Test
>>>>     public void someTest() throws Exception {
>>>>         sampleService.methodUnderTest();
>>>>     }
>>>> 
>>>> 
>>>> 
>>>> However when I use maven to run the tests I get the following exception. 
>>>>     Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed:
>>>> 0.183 sec <<<     FAILURE!
>>>>     org.sample.SamplelTest  Time elapsed: 0.182 sec  <<<
>>>> 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 & 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-->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
>>>> 
>>>> 
>>>> 
>>>> 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?
>>>> 
>>>> Here is my pom.xml:
>>>> <dependency>
>>>>         <groupId>org.aspectj</groupId>
>>>>         <artifactId>aspectjrt</artifactId>
>>>>         <version>1.6.11</version>
>>>> </dependency>
>>>> <build>
>>>>     <plugins>
>>>>         <plugin>
>>>>             <groupId>org.codehaus.mojo</groupId>
>>>>            
>>>> <artifactId>aspectj-maven-plugin</artifactId>
>>>>             <version>1.4</version>
>>>>             <configuration>
>>>>                 <source>1.6</source>
>>>>                 <target>1.6</target>
>>>>             </configuration>
>>>>             <executions>
>>>>                 <execution>
>>>>                     <phase>process-sources</phase>
>>>>                     <goals>
>>>>                         <goal>compile</goal>
>>>>                         <goal>test-compile</goal>
>>>>                     </goals>
>>>>                 </execution>
>>>>             </executions>
>>>>         </plugin>
>>>>     </plugins>
>>>> </build>
>>>> 
>>>> 
>>>> 
>>>> Any idea?
>>>> 
>>>> _______________________________________________
>>>> aspectj-users mailing list
>>>> aspectj-users@xxxxxxxxxxx <mailto:aspectj-users@xxxxxxxxxxx> 
>>>> To change your delivery options, retrieve your password, or unsubscribe from this list, visit
>>>> https://dev.eclipse.org/mailman/listinfo/aspectj-users <https://dev.eclipse.org/mailman/listinfo/aspectj-users> 
>>> 
>>> 
>>> _______________________________________________
>>> aspectj-users mailing list
>>> aspectj-users@xxxxxxxxxxx <mailto:aspectj-users@xxxxxxxxxxx> 
>>> To change your delivery options, retrieve your password, or unsubscribe from this list, visit
>>> https://dev.eclipse.org/mailman/listinfo/aspectj-users <https://dev.eclipse.org/mailman/listinfo/aspectj-users> 
>> 
>> 
>> _______________________________________________
>> aspectj-users mailing list
>> aspectj-users@xxxxxxxxxxx <mailto:aspectj-users@xxxxxxxxxxx> 
>> To change your delivery options, retrieve your password, or unsubscribe from this list, visit
>> https://dev.eclipse.org/mailman/listinfo/aspectj-users
> 
>


Back to the top