Hi Björn,

don't know if this directly addresses your problem, but we've had some
"fun" with Mockito and Felix in the past.

Please have a look at http://team.ops4j.org/browse/PAXEXAM-274, in
particular at the very last comment and the Javadoc in the test case
linked from there.

Best regards,
Harald

2012/7/31 Björn Pollex <[email protected]>:
> Hello Everyone!
>
> I have developed a small bundle that can create mocks using Mockito and
> register these as services. The problem is that sometimes I get mysterious
> exceptions:
>
>> ERROR: JarContent: Unable to read bytes. (java.lang.IllegalStateException:
>> zip file closed)
>> java.lang.IllegalStateException: zip file closed
>>     at java.util.zip.ZipFile.ensureOpen(ZipFile.java:415)
>>     at java.util.zip.ZipFile.getEntry(ZipFile.java:160)
>>     at org.apache.felix.framework.util.ZipFileX.getEntry(ZipFileX.java:52)
>>     at
>> org.apache.felix.framework.cache.JarContent.getEntryAsBytes(JarContent.java:122)
>>     at
>> org.apache.felix.framework.ModuleImpl$ModuleClassLoader.findClass(ModuleImpl.java:1856)
>>     at
>> org.apache.felix.framework.ModuleImpl.findClassOrResourceByDelegation(ModuleImpl.java:752)
>>     at
>> org.apache.felix.framework.ModuleImpl.access$400(ModuleImpl.java:72)
>>     at
>> org.apache.felix.framework.ModuleImpl$ModuleClassLoader.loadClass(ModuleImpl.java:1807)
>>     at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
>>     at java.lang.Class.forName0(Native Method)
>>     at java.lang.Class.forName(Class.java:169)
>>     at
>> org.mockito.internal.configuration.ClassPathLoader.loadConfiguration(ClassPathLoader.java:20)
>>     at
>> org.mockito.internal.configuration.GlobalConfiguration.createConfig(GlobalConfiguration.java:38)
>>     at
>> org.mockito.internal.configuration.GlobalConfiguration.<init>(GlobalConfiguration.java:32)
>>     at
>> org.mockito.internal.configuration.GlobalConfiguration.validate(GlobalConfiguration.java:47)
>>     at
>> org.mockito.internal.progress.MockingProgressImpl.validateMostStuff(MockingProgressImpl.java:81)
>>     at
>> org.mockito.internal.progress.MockingProgressImpl.validateState(MockingProgressImpl.java:68)
>>     at
>> org.mockito.internal.progress.ThreadSafeMockingProgress.validateState(ThreadSafeMockingProgress.java:49)
>>     at org.mockito.internal.MockHandler.handle(MockHandler.java:71)
>>     at
>> org.mockito.internal.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:36)
>>     at
>> org.mockito.internal.creation.MethodInterceptorFilter.intercept(MethodInterceptorFilter.java:48)
>>     at
>> de.younicos.osgi.testutilities.mocks.fooservice.Foo$$EnhancerByMockitoWithCGLIB$$56fd3ef0.finalize(<generated>)
>>     at java.lang.ref.Finalizer.invokeFinalizeMethod(Native Method)
>>     at java.lang.ref.Finalizer.runFinalizer(Finalizer.java:83)
>>     at java.lang.ref.Finalizer.access$100(Finalizer.java:14)
>>     at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:160)
>
>
> I do not exactly know what causes it, and it seems to happen randomly. In
> some cases a similar error happens when verification of mock fails, which
> results in the termination of the entire test-run (which effectively makes
> this approach unusable). I figured this was some kind of class-loading
> conflict between OSGi and Mockito, so I tried to provide Mockito via
> boot-delegation to make sure it always available everywhere. When running my
> test, Pax Exam shows the following logs (excerpt):
>
>> [...]
>> 15:00:46.082 [main] DEBUG o.o.p.e.n.i.NativeTestContainer -
>> org.osgi.framework.bootdelegation = sun.*,org.mockito,org.mockito.*
>> [...]
>> 15:00:46.082 [main] DEBUG o.o.p.e.n.i.NativeTestContainer -
>> java.class.path = [...]C:\Dokumente und
>> Einstellungen\pollexb\.m2\repository\org\mockito\mockito-all\1.9.0\mockito-all-1.9.0.jar;[...]
>
>
> So, boot-delegation is set up correctly, and Mockito appears to be on the
> class-path. Yet, none of the bundles using Mockito can load any class from
> any of these bundles:
>
>> java.lang.NoClassDefFoundError: org/mockito/Mockito
>>     at
>> de.younicos.osgi.testutilities.MockProvider.createRequestedMocks(MockProvider.java:73)
>>     at
>> de.younicos.osgi.testutilities.MockProvider.start(MockProvider.java:63)
>>     at
>> org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:629)
>>     at org.apache.felix.framework.Felix.activateBundle(Felix.java:1904)
>>     at org.apache.felix.framework.Felix.startBundle(Felix.java:1822)
>>     at
>> org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1192)
>>     at
>> org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:266)
>>     at java.lang.Thread.run(Thread.java:662)
>> Caused by: java.lang.ClassNotFoundException: org.mockito.Mockito not found
>> by de.younicos.osgi.testutilities.MockProvider [19]
>>     at
>> org.apache.felix.framework.ModuleImpl.findClassOrResourceByDelegation(ModuleImpl.java:812)
>>     at
>> org.apache.felix.framework.ModuleImpl.access$400(ModuleImpl.java:72)
>>     at
>> org.apache.felix.framework.ModuleImpl$ModuleClassLoader.loadClass(ModuleImpl.java:1807)
>>     at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
>>     ... 8 more
>
>
> The delegation happens (I verified it with the debugger), only the
> boot-classloader cannot find the packages. Can anyone explain why that is?
>
> As an alternative, I have tried providing all the packages using the
> systemPackages-option (which is probably the cleaner option anyway). When
> doing this it gets really strange. The first test I run, always works as
> expected. That means, if I run all of my tests individually (e.g. by
> commenting out the others), all tests pass. If I run more than one test,
> only the first one works as expected, and all the others fail, because the
> mock is not created. The bundle that should create the mocks throws this
> exception:
>
>> java.lang.ClassCastException
>>     at java.lang.Class.cast(Class.java:2990)
>>     at
>> org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:51)
>>     at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:52)
>>     at org.mockito.internal.MockitoCore.mock(MockitoCore.java:41)
>>     at org.mockito.Mockito.mock(Mockito.java:1014)
>>     at org.mockito.Mockito.mock(Mockito.java:909)
>>     at
>> de.younicos.osgi.testutilities.MockProvider.createRequestedMocks(MockProvider.java:74)
>>     at
>> de.younicos.osgi.testutilities.MockProvider.start(MockProvider.java:64)
>>     at
>> org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:629)
>>     at org.apache.felix.framework.Felix.activateBundle(Felix.java:1904)
>>     at org.apache.felix.framework.Felix.startBundle(Felix.java:1822)
>>     at
>> org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1192)
>>     at
>> org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:266)
>>     at java.lang.Thread.run(Thread.java:662)
>
>
> I am at a complete loss to explain this behavior. Does anyone have any
> ideas?
>
> The problem is rather complex, but if it help, I will take the time to
> create a test-project on GitHub. I would have done that anyway, since my
> plan was to use this bundle as a basis for adding built-in service-mocking
> to Pax Exam (I will post a proposal for this soonish, once I fixed this
> problem).
>
> I would be glad for any pointer anyone could provide.
>
> Greetings,
>
>     Björn Pollex
>
> _______________________________________________
> general mailing list
> [email protected]
> http://lists.ops4j.org/mailman/listinfo/general
>

_______________________________________________
general mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/general

Reply via email to