dhamini-poornachandra / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

De-Serialization does not work in a different jvm / classloader #387

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. create mock withSettings().serializable();
2. serialize mock instance into a file
3. de-serialize mock from file in a different jvm

What is the expected output? What do you see instead?
expected: recreate mock instance including behaviour.
instead: class not found exception because of cglib enhanced class type that is 
not known in the target jvm. (in the same jvm / classloader everthing is fine)

What version of the product are you using? On what operating system?
ubuntu 11.04, mockito 1.9.5, java 1.6

Please provide any additional information below.

The problem is the general de-serialization of cglib enhanced objects, because 
of the dynamically created subclasses that are unknown on the target jvm.
This aspect has to be taken into account, when creating serializale mocks.
One has to unwrap the mocked class / interface first, serialize the "state" of 
the mock and then at de-serialization recreate the proxy wrapper again.

If mock cant be deserialized in a different jvm (an application server) the 
serialization feature isnt very useful to me.

Original issue reported on code.google.com by ske...@gmail.com on 23 Oct 2012 at 7:17

GoogleCodeExporter commented 8 years ago
Well I don't think there's a solution for that yet, unless you have a 
deserializer that knows better about CGLIB classes.
It's actually expected, CGLIB are generated in memory using a custom 
classloader to pass over the bytecode to the JVM. In a new JVM, if the CGLIB 
class is not found then or even if Mockito isn't in the class path it couldn't 
even work.

At the moment I'm not sure if it's even doable using the standard serialization 
API, though it might be worth investigating writeReplace / readResolve, but 
you'll need Mockito in the classpath on both JVM of course.

Original comment by brice.du...@gmail.com on 23 Oct 2012 at 8:44

GoogleCodeExporter commented 8 years ago
Yes mockito must be on the classpath on both JVM. There are a few posts out 
there like 
http://tech.thecoolblogs.com/2010/12/how-to-serialize-and-de-serialize-cglib.htm
l
or http://i-proving.com/2008/02/11/the-pitfalls-of-dynamic-proxy-serialization/
discussing the deserialization of cg proxies. One possible solution is indeed 
to override writeReplace / readResolve and thereby recreate the cg proxy 
wrapper on deserialization.
IHMO mockito could / should do it that way, and hopefully deserialization on an 
application server will work.

Original comment by ske...@gmail.com on 23 Oct 2012 at 10:02

GoogleCodeExporter commented 8 years ago
Cool, thx for the links :)

I'm a bit busy atm, I'm not sure I'll have time to work on this feature in near 
time, but if you'd like to begin some work we'll be glad to help and integrate 
it if it's working properly and follows Mockito principles.

Original comment by brice.du...@gmail.com on 23 Oct 2012 at 11:15

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 27 Nov 2012 at 5:08

GoogleCodeExporter commented 8 years ago
Hi,

Just to let you know that I got something working, due to the nature of mocks 
there are a few other stuff to do. Strangely enough I completely forgot your 
links when working on it, and I found they followed the same approach I took, 
although there was some other challenges to just make the serialization work 
with mocks.

When I'll have more free time I will address the remaining issues.

Also keep in mind there could be issues that I didn't have foreseen atm. So it 
might not be perfect.

Cheers,
Brice

Original comment by brice.du...@gmail.com on 3 Dec 2012 at 9:25

GoogleCodeExporter commented 8 years ago
Hey,

Take a look at our github, I recently merged the code that performs 
serialization in a way that enable a mock to be deserialized in another 
classloader / JVM 
https://github.com/mockito/mockito/commit/eaea9467f546f37fbeb16b128808d791a7836e
b9

It would be awesome if you try it, report issues, performance, etc...

Cheers,
Brice

Original comment by brice.du...@gmail.com on 8 Jan 2013 at 4:46

GoogleCodeExporter commented 8 years ago
Issue 268 has been merged into this issue.

Original comment by brice.du...@gmail.com on 8 Jan 2013 at 4:49

GoogleCodeExporter commented 8 years ago
Issue 268 has been merged into this issue.

Original comment by brice.du...@gmail.com on 8 Feb 2013 at 2:51

GoogleCodeExporter commented 8 years ago

Original comment by szcze...@gmail.com on 16 Aug 2014 at 2:43

GoogleCodeExporter commented 8 years ago

Original comment by szcze...@gmail.com on 24 Aug 2014 at 3:14

GoogleCodeExporter commented 8 years ago

Original comment by szcze...@gmail.com on 24 Aug 2014 at 3:50