Mocking dynamic types

View: New views
2 Messages — Rating Filter:   Alert me  

Mocking dynamic types

by Julian Hall-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm working on some code at the moment that heavily uses reflection, and
as such the interfaces of objects that are passed to it are defined more
in terms of conventions than classes... for example, it can take an
object and the name of a property of that object and manipulate the
property without needing to know anything about the class of the object.

As such, I'm finding that the things I need the mock objects in my tests
to support don't actually conform to any existing classes in my
application.  Instead, I need things like "an object that's a subclass
of Entity but has a method called getTestProperty returning int" or
similar.

Now, I could just define such a class, then mock it, but this seems like
a messy solution.  It seems to me that as jmock is building classes
dynamically already, it ought to be possible to extend it reasonably
easily so that I can request extra methods in the mock objects it
creates.  But I have little idea about how to go about this.  Is there a
suitable extension point already?  How would such an extension integrate
with the framework?  I'd appreciate any suggestions.

Jules

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

    http://xircles.codehaus.org/manage_email



Re: Mocking dynamic types

by Nat Pryce :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This isn't something that jMock is designed for.

But, we have had to do that to test jMock itself.  Have a look at the
MethodFactory class in the tests.  You might be able to adapt that
code to your use.

--Nat

2009/6/27 Julian Hall <jules@...>:

> I'm working on some code at the moment that heavily uses reflection, and as
> such the interfaces of objects that are passed to it are defined more in
> terms of conventions than classes... for example, it can take an object and
> the name of a property of that object and manipulate the property without
> needing to know anything about the class of the object.
>
> As such, I'm finding that the things I need the mock objects in my tests to
> support don't actually conform to any existing classes in my application.
>  Instead, I need things like "an object that's a subclass of Entity but has
> a method called getTestProperty returning int" or similar.
> Now, I could just define such a class, then mock it, but this seems like a
> messy solution.  It seems to me that as jmock is building classes
> dynamically already, it ought to be possible to extend it reasonably easily
> so that I can request extra methods in the mock objects it creates.  But I
> have little idea about how to go about this.  Is there a suitable extension
> point already?  How would such an extension integrate with the framework?
>  I'd appreciate any suggestions.
>
> Jules
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>   http://xircles.codehaus.org/manage_email
>
>
>



--
http://www.natpryce.com

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

    http://xircles.codehaus.org/manage_email