« Return to Thread: grabbing value from mutator on mockobject?

Re: grabbing value from mutator on mockobject?

by Julian Hall-2 :: Rate this Message:

Reply to Author | View in Thread

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


 « Return to Thread: grabbing value from mutator on mockobject?