Java Mailing List Archive

http://www.gg3721.com/

Home » user.jmock »

Re: [jmock-user] Ignoring calls to static methods

Steve Freeman

2010-02-09

Replies: Find Java Web Hosting

Author LoginPost Reply
There are other frameworks that do that sort of thing. I tend to think it's a bad idea. The relevant point here is that the B singleton is a dependency. You can either hide it in A or make it explicit and pass it in, perhaps when A is constructed. Whether you need to wrap B depends on what it is and what you do with it.

S

On 9 Feb 2010, at 14:04, Panos Konstantinidis wrote:
> yes I do have access to class A, this is the class I wrote. And you are right, I could introduce an object C, that wraps access to class B. But again there are two concerns here:
>
> a) I introduce a third object just to test my first object, and
> b) I need to change the source code of class A, because I need a way to pass this wrapper object to the mockery context, otherwise I couldn't be able to set expectations on it and mock the functionality. So, as you say I will need to test it in an integration test, which is already being done for the current class B.
>
> I was wondering if there is a simple way, something like (if "context" is of type Mockery)
>
> mockery.ignoreClass(B.class);
>
> I know that JMock uses reflection to mock interfaces JMock works, but I guess, since it's using cglib, it has some interaction at byte code level. My original thought was that maybe someone knows how to bypass byte code calls through JMock.
>
> Regards
>
> Panos
>
> --- On Tue, 2/9/10, steve@(protected):
>
> If you have access to class A, you could consider wrapping the access to class B in an object (c) that will return the instance. That breaks the static dependency and makes the code testable. You can then test the implementation of (c) in an integration test, if it's at all complicated, or just let the acceptance tests exercise it.
>
> Another option to consider is whether you could pass an instance of B into A, perhaps on construction. Again, this doesn't make the probably totally disappear, but makes A's dependencies clearer and easier to work with.
>
> S.
>
>
>

Steve Freeman

Winner of the Agile Alliance Gordon Pask award 2006
Book: http://www.growing-object-oriented-software.com

+44 (0) 797 179 4105
M3P Limited. http://www.m3p.co.uk
Registered office. 2 Church Street, Burnham, Bucks, SL1 7HZ.
Company registered in England & Wales. Number 03689627




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

  http://xircles.codehaus.org/manage_email


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