[TIP] mock-ing java classes in Jython?
fredrik at haard.se
Mon Aug 1 04:24:30 PDT 2011
It seems that you are right; but since I want to avoid static magic in the
device handler, I think I'll have to shuffle the Java around a bit.
Den söndagen den 31:e juli 2011 skrev Michael Foord<
fuzzyman at voidspace.org.uk>:
> On 31 Jul 2011, at 11:23, Fredrik Håård wrote:
>> Hi all (and thanks for the pizza! =),
>> I'd like to set up a test suite for an embedded J2ME application. Most of
the code is cleanly separated from the platform since it runs on multiple
platforms, and I wanted to use Jython to replace the platform-specific parts
and run both unit and scenario testing on the common parts.
>> However, naive replacement of the class definitions does not seem to
work, and trying to use mock to patch a java class like this:
>> with mock.patch('my.device.DeviceHandler') as devicehandler:
>> i get:
>> AttributeError: 'javapackage' object has no attribute 'DeviceHandler'
>> Is it possible to get the behavior I want?
> I don't use python, but I assume that in Jython you can't replace
arbitrary attributes on modules written in Java - just as you can't replace
arbitrary attributes on modules written in C on CPython.
> A way round this would be to import 'DeviceHandler' into your module
(instead of accessing it as 'my.device.DeviceHandler' just use
DeviceHandler). Then you can just patch 'DeviceHandler' in the module you
use it rather than having to patch it on the package that contains it.
> All the best,
> Michael Foord
> May you do good and not evil
> May you find forgiveness for yourself and forgive others
> May you share freely, never taking more than you give.
> -- the sqlite blessing
I reject your reality and substitute my own.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the testing-in-python