Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Without a stacktrace I'll assume you have a <code>net.sf.cglib.core.CodeGenerationException</code> probably with cause like <code>InvocationTargetException</code> or <code>ClassFormatError</code>, </p> <p>And that happen because CGLIB can't enhance an already enhanced class that it created itself. As Mockito is using CGLIB internally on the JVM, you cannot enhance a Mockito class. you need to enhance the original class. <em>Plus even if it was possible the callback is an instance you need pass to the generated class, so by creating your own enhancer you don't have the Mockito callback for the upper class. So the mockito mock functionality won't even work. Anyway, I'm diverging from the topic.</em></p> <p>So basically you cannot enhance an enhanced class with CGLIB, if this issue is happening at runtime because you passed a mock, I believe you should have a code like this, check if it's enhanced, if yes use the superclass (the original class) and/or the interfaces :</p> <pre><code>public class CGLIBEnhancerEnhancer implements TypeEnhancer { public void Object enhance(Object objectCandidateToEnhance, MethodInterceptor interceptor) { Class classCandidateToEnhance = classCandidateToEnhance.getClass(); if(Enhancer.isEnhanced(classCandidateToEnhance) || Mockito.mockingDetails(objectCandidateToEnhance).isMock()) { // safe with CGLIB (2.x) enhanced class return (Bar) Enhancer.create( classCandidateToEnhance.getSuperclass(), classCandidateToEnhance.getInterfaces(), interceptor ); } else return (Bar) Enhancer.create(classCandidateToEnhance, interceptor); } } } </code></pre> <p><strong>EDIT:</strong> I ran the given example and it indeed give me the <code>CodeGenerationException</code>, that you can see at the end of this answer. Though maybe depending on the environment you can see a different stacktrace like the one you posted.</p> <p>Given your exception I believe you might have an issue at runtime of how the double enhanced class is created. As the stacktrace suggest that the instance doesn't even have a foo method, it's like the object (which seems to be a real mockito mock) was generated from the wrong type. I would look at the API of the Enhancer to use correctly the type and interfaces to create the enhanced class, you could use a new instance of the <code>Enhancer</code> or use static methods.</p> <p>Also if you are proving the interceptor yourself, and that you want to execute the behavior of the given instance (mock or not), you should craft your interceptor such that it will hold a reference to the original object.</p> <p><strong>EDIT 2:</strong> Actually Mockito is repackaging/jarjar/inline CGLIB due to technical reasons, this means that <code>net.sf.cglib.proxy.Enhancer</code> cannot detect a mockito mock. Instead one should use the newly introduced API in 1.9.5 <code>Mockito.mockingDetails(instance).isMock()</code> to detect a mockito mock. Or use the repackaged/jarjared/inlined <code>org.mockito.cglib.proxy.Enhancer</code>. In the end you'll need Mockito in your classpath to detect a Mockito mock.</p> <p>Hope that helps</p> <pre><code>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.create(Enhancer.java:286) at org.mockito.cglib.proxy.Enhancer.create(Enhancer.java:664) at ATest.verifyInterceptorIsInvokedOnCallToObjectOfEnhanced(ATest.java:32) at ATest.cannotCallMethodOnMockWrapper(ATest.java:18) ... removed at org.junit.runner.JUnitCore.run(JUnitCore.java:157) ... removed Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at org.mockito.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:385) at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:220) ... 31 more Caused by: java.lang.ClassFormatError: Duplicate method name&amp;signature in class file ATest$Bar$$EnhancerByMockitoWithCGLIB$$58a2468b$$EnhancerByCGLIB$$9232d1df at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) at java.lang.ClassLoader.defineClass(ClassLoader.java:615) ... 37 more </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload