Java Mailing List Archive

http://www.gg3721.com/

Home » user.jmock »

[jmock-user] 1.2.0 no longer can mock non-public interfaces

alex-pub jmock

2008-02-22

Replies: Find Java Web Hosting

Author LoginPost Reply
I have a test case where I need to mock a class (not under my control) and
some interfaces (under my control). The interfaces under my control are
declared package private.

Also my test case used to work fine in 1.1.0.

In 1.2.0 I get the following exception:
net.sf.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
 at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:237)
 at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
 at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:304)
 at org.jmock.cglib.CGLIBCoreMock.<init>(CGLIBCoreMock.java:63)
 at org.jmock.cglib.CGLIBCoreMock.<init>(CGLIBCoreMock.java:46)
 at org.jmock.cglib.CGLIBCoreMock.<init>(CGLIBCoreMock.java:35)
 at org.jmock.cglib.MockObjectTestCase.newCoreMock(MockObjectTestCase.java:33)
 at org.jmock.MockObjectTestCase.mock(MockObjectTestCase.java:67)
 at com.rfxtech.io.TestInputStreamDataCollector.setUp(TestInputStreamDataCollector.java:114)
 at org.jmock.core.VerifyingTestCase.runBare(VerifyingTestCase.java:37)
 at junit.framework.TestResult$1.protect(TestResult.java:106)
 at junit.framework.TestResult.runProtected (TestResult.java:124)
 at junit.framework.TestResult.run (TestResult.java:109)
 at junit.framework.TestCase.run (TestCase.java:120)
 at junit.framework.TestSuite.runTest (TestSuite.java:230)
 at junit.framework.TestSuite.run (TestSuite.java:225)
 at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner.java:460)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner.java:673)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (RemoteTestRunner.java:386)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (RemoteTestRunner.java:196)
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 java.lang.reflect.Method.invoke (Method.java:324)
 at net.sf.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:384)
 at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:219)
 ... 21 more
Caused by: java.lang.IllegalAccessError: class org.jmock.codegen.com.rfxtech.io.InputStreamDataCollector$IInputStreamMonitor$$EnhancerByCGLIB$$96ee6534 cannot access its superinterface com.rfxtech.io.InputStreamDataCollector$IInputStreamMonitor
 at java.lang.ClassLoader.defineClass0(Native Method)
 at java.lang.ClassLoader.defineClass (ClassLoader.java:539)
 ... 27 more
   

I see that the route cause of the exception is that the enhanced class is
in the wrong package 'org.jmock.codegen.com.rfxtech...' instead of
'com.rfxtech...'

I think that I tracked it to a change in CGLIBCoreMock:

  private static NamingPolicy NAMING_POLICY = new DefaultNamingPolicy() {
    public String getClassName(String prefix, String source, Object key, Predicate names) {
       return "org.jmock.codegen." + super.getClassName(prefix, source, key, names);
    }
  };
 
    Enhancer enhancer = new Enhancer();
    enhancer.setClassLoader(mockedType.getClassLoader());
    enhancer.setSuperclass(mockedType);
    enhancer.setCallback(this);
    >>enhancer.setNamingPolicy(NAMING_POLICY);
   
I have a few questions about this:

1. Why new code is necessary?
2. Can you append 'org.jmock.codegen' instead of prepending it?
3. Is there a workaround for this?
4. Does this behavior manifest itself in 2.4.0, as I am planning to switch
to it eventually?


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


©2008 gg3721.com - Jax Systems, LLC, U.S.A.