grabbing value from mutator on mockobject?

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

grabbing value from mutator on mockobject?

by nino martinez wael :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi

I might be headed down a wrong path.. But I have something like this:

                                KeySetup keySetupCpr=mockery.mock(KeySetup.class);
                                one(readOnlyAllInOneService).getKeySetup(KeySetupIdentification.class,
"1010", "CustomCustomerIdent");
                                will(returnValue(keySetupCpr));
                                one(keySetupCpr).setTerminators(with(any(List.class)));

Now my keysetup are being called with a list and the next thing that
happens are a call to get keySetup.getTerminators , are there any way
I can get the list that has just been set and return that?

regards Nino

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

    http://xircles.codehaus.org/manage_email



Re: grabbing value from mutator on mockobject?

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Sorry, I need to clarify.

The service returns a KeySetup?
And you give the KeySetup a list to fill in?
Then you call getTerminators to return a List?

What's in KeySetup? Would you be better off just using a real object?

Why do you set the list and then retrieve it?

S.


On 11 Jun 2009, at 13:26, nino martinez wael wrote:

> Hi
>
> I might be headed down a wrong path.. But I have something like this:
>
> KeySetup keySetupCpr=mockery.mock(KeySetup.class);
>
> one(readOnlyAllInOneService).getKeySetup(KeySetupIdentification.class,
> "1010", "CustomCustomerIdent");
> will(returnValue(keySetupCpr));
> one(keySetupCpr).setTerminators(with(any(List.class)));
>
> Now my keysetup are being called with a list and the next thing that
> happens are a call to get keySetup.getTerminators , are there any way
> I can get the list that has just been set and return that?
>
> regards Nino
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

Steve Freeman
Winner of the Agile Alliance Gordon Pask award 2006

http://www.m3p.co.uk

M3P Limited.
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



Re: grabbing value from mutator on mockobject?

by nino martinez wael :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Steve, replying inline...

2009/6/11 Steve Freeman <steve@...>:
> Sorry, I need to clarify.
>
> The service returns a KeySetup?
Yes..
> And you give the KeySetup a list to fill in?
Yes.

> Then you call getTerminators to return a List?
Some other part of what I am testing yes.
>
> What's in KeySetup? Would you be better off just using a real object?
Only simple stuff, but yes I am wondering if I would be better off
using the real object..
>
> Why do you set the list and then retrieve it?
Again it's some of the underlying stuff that does that.

>
> S.
>
>
> On 11 Jun 2009, at 13:26, nino martinez wael wrote:
>>
>> Hi
>>
>> I might be headed down a wrong path.. But I have something like this:
>>
>>                                KeySetup
>> keySetupCpr=mockery.mock(KeySetup.class);
>>
>>  one(readOnlyAllInOneService).getKeySetup(KeySetupIdentification.class,
>> "1010", "CustomCustomerIdent");
>>                                will(returnValue(keySetupCpr));
>>
>>  one(keySetupCpr).setTerminators(with(any(List.class)));
>>
>> Now my keysetup are being called with a list and the next thing that
>> happens are a call to get keySetup.getTerminators , are there any way
>> I can get the list that has just been set and return that?
>>
>> regards Nino
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>   http://xircles.codehaus.org/manage_email
>>
>>
>
> Steve Freeman
> Winner of the Agile Alliance Gordon Pask award 2006
>
> http://www.m3p.co.uk
>
> M3P Limited.
> 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
>
>
>

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

    http://xircles.codehaus.org/manage_email



Re: grabbing value from mutator on mockobject?

by nino martinez wael :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The reason that I asked this question anyhow was because I wanted to
use the real object but I cant grab it since Im not sure howto do that
with jmock..

For example here I expect this:

                                one(keySetupCpr).setTerminators(with(any(List.class)));

if I could grab that list things would be easier...

2009/6/11 nino martinez wael <nino.martinez.wael@...>:

> Hi Steve, replying inline...
>
> 2009/6/11 Steve Freeman <steve@...>:
>> Sorry, I need to clarify.
>>
>> The service returns a KeySetup?
> Yes..
>> And you give the KeySetup a list to fill in?
> Yes.
>
>> Then you call getTerminators to return a List?
> Some other part of what I am testing yes.
>>
>> What's in KeySetup? Would you be better off just using a real object?
> Only simple stuff, but yes I am wondering if I would be better off
> using the real object..
>>
>> Why do you set the list and then retrieve it?
> Again it's some of the underlying stuff that does that.
>>
>> S.
>>
>>
>> On 11 Jun 2009, at 13:26, nino martinez wael wrote:
>>>
>>> Hi
>>>
>>> I might be headed down a wrong path.. But I have something like this:
>>>
>>>                                KeySetup
>>> keySetupCpr=mockery.mock(KeySetup.class);
>>>
>>>  one(readOnlyAllInOneService).getKeySetup(KeySetupIdentification.class,
>>> "1010", "CustomCustomerIdent");
>>>                                will(returnValue(keySetupCpr));
>>>
>>>  one(keySetupCpr).setTerminators(with(any(List.class)));
>>>
>>> Now my keysetup are being called with a list and the next thing that
>>> happens are a call to get keySetup.getTerminators , are there any way
>>> I can get the list that has just been set and return that?
>>>
>>> regards Nino
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>>   http://xircles.codehaus.org/manage_email
>>>
>>>
>>
>> Steve Freeman
>> Winner of the Agile Alliance Gordon Pask award 2006
>>
>> http://www.m3p.co.uk
>>
>> M3P Limited.
>> 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
>>
>>
>>
>

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

    http://xircles.codehaus.org/manage_email



Re: grabbing value from mutator on mockobject?

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 11 Jun 2009, at 14:13, nino martinez wael wrote:
> The reason that I asked this question anyhow was because I wanted to
> use the real object but I cant grab it since Im not sure howto do that
> with jmock..
>
> For example here I expect this:
>
> one(keySetupCpr).setTerminators(with(any(List.class)));
>
> if I could grab that list things would be easier...


You can (using the with() clause). But I think your test is trying to  
tell about a problem with the current design. I'm not sure what it is,  
but all this passing around of lists doesn't sound right.

Either that, or the unit test is working with too large a unit.

S.

Steve Freeman
Winner of the Agile Alliance Gordon Pask award 2006

http://www.m3p.co.uk

M3P Limited.
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



Re: grabbing value from mutator on mockobject?

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 11 Jun 2009, at 14:10, nino martinez wael wrote:
>> Then you call getTerminators to return a List?
> Some other part of what I am testing yes.
>>
>> What's in KeySetup? Would you be better off just using a real object?
> Only simple stuff, but yes I am wondering if I would be better off
> using the real object..
>>
>> Why do you set the list and then retrieve it?
> Again it's some of the underlying stuff that does that.


So, maybe you're testing too large a chunk in this test?

S

Steve Freeman
Winner of the Agile Alliance Gordon Pask award 2006

http://www.m3p.co.uk

M3P Limited.
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



Re: grabbing value from mutator on mockobject?

by Julian Hall-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

nino martinez wael wrote:

> The reason that I asked this question anyhow was because I wanted to
> use the real object but I cant grab it since Im not sure howto do that
> with jmock..
>
> For example here I expect this:
>
> one(keySetupCpr).setTerminators(with(any(List.class)));
>
> if I could grab that list things would be easier...
>  
My solution to this kind of requirement is this class (which anyone
should feel free to use however they want, just make sure you give me
credit if distributing in source form):

--- file: MockProperty.java
import org.jmock.api.Action;
import org.jmock.api.Invocation;
import org.jmock.lib.action.CustomAction;

public class MockProperty<T>
{
    String   name;
    Class<T> clazz;
    T        value;

    protected MockProperty (String name, Class<T> clazz)
    {
        this.name = name;
        this.clazz = clazz;
    }

    public void setValue (T newValue)
    {
        value = newValue;
    }

    public T getValue ()
    {
        return value;
    }

    public Action setValueFromParameter (int index)
    {
        return new SetterAction (index);
    }

    public Action returnValue ()
    {
        return new GetterAction ();
    }

    private class SetterAction extends CustomAction
    {
        int index;

        public SetterAction (int index)
        {
            super ("set " + name + " to value in parameter " + index);
            this.index = index;
        }

        @Override
        public Object invoke (Invocation invocation) throws Throwable
        {
            value = clazz.cast (invocation.getParameter (index));
            return null;
        }
    }
   
    private class GetterAction extends CustomAction
    {
        public GetterAction ()
        {
            super ("return value stored in " + name);
        }

        @Override
        public Object invoke (Invocation invocation) throws Throwable
        {
            return value;
        }
    }
   
    public static <T> MockProperty<T> create (String name, Class<T> clazz)
    {
        return new MockProperty<T> (name, clazz);
    }
}

---- end of file

This both serves as a test for it, and shows how to use it:

---- file: MockPropertyTest.java

import static org.junit.Assert.*;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Test;


public class MockPropertyTest
{
    interface TestInterface
    {
        public void set (int i);

        public int get ();
    }

    @Test
    public void setAndReturnValueWorks ()
    {
        Mockery mockery = new Mockery ();
        final MockProperty<Integer> testProperty = MockProperty.create
("testProperty", Integer.class);
        final TestInterface testInterface = mockery.mock
(TestInterface.class);
        mockery.checking (new Expectations () {
            {
                one (testInterface).set (with (any (Integer.class)));
                will (testProperty.setValueFromParameter (0));
                one (testInterface).get ();
                will (testProperty.returnValue ());
            }
        });

        testInterface.set (42);
        assertEquals (42, (int) testProperty.getValue ());
        assertEquals (42, testInterface.get ());

        mockery.assertIsSatisfied ();
    }
}

---- end of file

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

    http://xircles.codehaus.org/manage_email



Re: grabbing value from mutator on mockobject?

by nino martinez wael :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hmm yeah I cant decide.. But it certainly feels something like it..
But it would sure be nice to have covered..

2009/6/11 Steve Freeman <steve@...>:

> On 11 Jun 2009, at 14:10, nino martinez wael wrote:
>>>
>>> Then you call getTerminators to return a List?
>>
>> Some other part of what I am testing yes.
>>>
>>> What's in KeySetup? Would you be better off just using a real object?
>>
>> Only simple stuff, but yes I am wondering if I would be better off
>> using the real object..
>>>
>>> Why do you set the list and then retrieve it?
>>
>> Again it's some of the underlying stuff that does that.
>
>
> So, maybe you're testing too large a chunk in this test?
>
> S
>
> Steve Freeman
> Winner of the Agile Alliance Gordon Pask award 2006
>
> http://www.m3p.co.uk
>
> M3P Limited.
> 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
>
>
>

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

    http://xircles.codehaus.org/manage_email



Re: grabbing value from mutator on mockobject?

by nino martinez wael :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks for the solution.:)

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

> nino martinez wael wrote:
>>
>> The reason that I asked this question anyhow was because I wanted to
>> use the real object but I cant grab it since Im not sure howto do that
>> with jmock..
>>
>> For example here I expect this:
>>
>>
>>  one(keySetupCpr).setTerminators(with(any(List.class)));
>>
>> if I could grab that list things would be easier...
>>
>
> My solution to this kind of requirement is this class (which anyone should
> feel free to use however they want, just make sure you give me credit if
> distributing in source form):
>
> --- file: MockProperty.java
> import org.jmock.api.Action;
> import org.jmock.api.Invocation;
> import org.jmock.lib.action.CustomAction;
>
> public class MockProperty<T>
> {
>   String   name;
>   Class<T> clazz;
>   T        value;
>
>   protected MockProperty (String name, Class<T> clazz)
>   {
>       this.name = name;
>       this.clazz = clazz;
>   }
>
>   public void setValue (T newValue)
>   {
>       value = newValue;
>   }
>
>   public T getValue ()
>   {
>       return value;
>   }
>
>   public Action setValueFromParameter (int index)
>   {
>       return new SetterAction (index);
>   }
>
>   public Action returnValue ()
>   {
>       return new GetterAction ();
>   }
>
>   private class SetterAction extends CustomAction
>   {
>       int index;
>
>       public SetterAction (int index)
>       {
>           super ("set " + name + " to value in parameter " + index);
>           this.index = index;
>       }
>
>       @Override
>       public Object invoke (Invocation invocation) throws Throwable
>       {
>           value = clazz.cast (invocation.getParameter (index));
>           return null;
>       }
>   }
>     private class GetterAction extends CustomAction
>   {
>       public GetterAction ()
>       {
>           super ("return value stored in " + name);
>       }
>
>       @Override
>       public Object invoke (Invocation invocation) throws Throwable
>       {
>           return value;
>       }
>   }
>     public static <T> MockProperty<T> create (String name, Class<T> clazz)
>   {
>       return new MockProperty<T> (name, clazz);
>   }
> }
>
> ---- end of file
>
> This both serves as a test for it, and shows how to use it:
>
> ---- file: MockPropertyTest.java
>
> import static org.junit.Assert.*;
>
> import org.jmock.Expectations;
> import org.jmock.Mockery;
> import org.junit.Test;
>
>
> public class MockPropertyTest
> {
>   interface TestInterface
>   {
>       public void set (int i);
>
>       public int get ();
>   }
>
>   @Test
>   public void setAndReturnValueWorks ()
>   {
>       Mockery mockery = new Mockery ();
>       final MockProperty<Integer> testProperty = MockProperty.create
> ("testProperty", Integer.class);
>       final TestInterface testInterface = mockery.mock
> (TestInterface.class);
>       mockery.checking (new Expectations () {
>           {
>               one (testInterface).set (with (any (Integer.class)));
>               will (testProperty.setValueFromParameter (0));
>               one (testInterface).get ();
>               will (testProperty.returnValue ());
>           }
>       });
>
>       testInterface.set (42);
>       assertEquals (42, (int) testProperty.getValue ());
>       assertEquals (42, testInterface.get ());
>
>       mockery.assertIsSatisfied ();
>   }
> }
>
> ---- end of file
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>   http://xircles.codehaus.org/manage_email
>
>
>

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

    http://xircles.codehaus.org/manage_email



Re: grabbing value from mutator on mockobject?

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

No, really. Take another look at your design, I think your test is  
trying to tell you something.

This code is fine, but you should be cautious about using this much  
behaviour in a mock. Having to hang on to an object so you can return  
elsewhere in a test is a hint that maybe you should use a real object,  
or that the object should be passed directly, or that there's a seam  
that hasn't been exposed.

S

On 12 Jun 2009, at 08:02, nino martinez wael wrote:

> Thanks for the solution.:)
>
> 2009/6/11 Julian Hall <jules@...>:
>> nino martinez wael wrote:
>>>
>>> The reason that I asked this question anyhow was because I wanted to
>>> use the real object but I cant grab it since Im not sure howto do  
>>> that
>>> with jmock..
>>>

Steve Freeman
Winner of the Agile Alliance Gordon Pask award 2006

http://www.m3p.co.uk

M3P Limited.
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



Re: grabbing value from mutator on mockobject?

by nino martinez wael :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I know, the problem are that the injection framework actually requires
the code to run on an webserver..  So as you say I need a mix of
things. And this is where Im at, defining what to mock and what not,
and further what is possible and what not. On top of that the
framework behind are not very good at using interfaces so that's just
another stone in my shoe:(

2009/6/12 Steve Freeman <steve@...>:

> No, really. Take another look at your design, I think your test is trying to
> tell you something.
>
> This code is fine, but you should be cautious about using this much
> behaviour in a mock. Having to hang on to an object so you can return
> elsewhere in a test is a hint that maybe you should use a real object, or
> that the object should be passed directly, or that there's a seam that
> hasn't been exposed.
>
> S
>
> On 12 Jun 2009, at 08:02, nino martinez wael wrote:
>>
>> Thanks for the solution.:)
>>
>> 2009/6/11 Julian Hall <jules@...>:
>>>
>>> nino martinez wael wrote:
>>>>
>>>> The reason that I asked this question anyhow was because I wanted to
>>>> use the real object but I cant grab it since Im not sure howto do that
>>>> with jmock..
>>>>
>
> Steve Freeman
> Winner of the Agile Alliance Gordon Pask award 2006
>
> http://www.m3p.co.uk
>
> M3P Limited.
> 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
>
>
>

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

    http://xircles.codehaus.org/manage_email