Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does cglib fail to call the MethodInterceptor callback?
    text
    copied!<p>In the unittest class show below, one of the tests fails and the other succeeds. Both tests create a <code>CGLIB</code> object with an interceptor mock and attempt to verify that the interceptor is interacted with. The tests differ on the class that is dynamically subclassed with CGLIB. The test that succeeds subclasses a plain Java interface; the one that fails subclasses a dynamic subclass (created with <code>Mockito</code>). Why is that first test failing?</p> <p>Thanks.</p> <pre><code>public class ATest { @Test // This test fails. public void cannotCallMethodOnMockWrapper() throws Throwable { final Class&lt;? extends Bar&gt; c = Mockito.mock(Bar.class).getClass(); verifyInterceptorIsInvokedOnCallToObjectOfEnhanced(c); } @Test // This test succeeds. public void interceptorIsCalled() throws Throwable { final Class&lt;? extends Bar&gt; c = Bar.class; verifyInterceptorIsInvokedOnCallToObjectOfEnhanced(c); } private void verifyInterceptorIsInvokedOnCallToObjectOfEnhanced( final Class&lt;? extends Bar&gt; c) throws Throwable { final MethodInterceptor interceptor = Mockito.mock( MethodInterceptor.class); final Bar wrapper = (Bar) Enhancer.create(c, interceptor); // Here is where the failing test chokes with exception: // NoSuchMethodError wrapper.foo(); verifyInterceptIsCalledOn(interceptor); } private void verifyInterceptIsCalledOn( final MethodInterceptor interceptor) throws Throwable { verify(interceptor).intercept(any(), any(Method.class), any(Object[].class), any(MethodProxy.class)); } public static interface Bar { void foo(); } } </code></pre> <p><strong>UPDATE:</strong></p> <p>The stack trace of the failure</p> <pre><code>java.lang.NoSuchMethodError: java.lang.Object.foo()V at ATest$Bar$$EnhancerByMockitoWithCGLIB$$2e1d601f.foo(&lt;generated&gt;) at ATest.verifyInterceptorIsInvokedOnCallToObjectOfEnhanced(ATest.java:37) at ATest.cannotCallMethodOnMockWrapper(ATest.java:19) </code></pre> <p>Also, as to whether a Mockito mock is a CGLIB-enhanced class, the test below (which fails) seems to suggest that it is not:</p> <pre><code>public class ATest { @Test public void mockitoMockIsCGLIBEnhanced() { assertTrue(Enhancer.isEnhanced(Mockito.mock(Bar.class).getClass())); } public static interface Bar { void foo(); } } </code></pre>
 

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