Eugeny N Dzhurinsky | 2 Nov 2007 12:08

verify properties of argument objects passed to methods of a mocked interface

Hello!

I would like to test the implementation of my custom TableModel (Swing). In
this model it is possible to fire events when something was changed in the
table. At this point TableModelListener interface is used, in particular -
it's method tableChanged which accepts TableModelEvent.

I am creating the mock for the TableModelListener and register it in
TableModel, then I would like to test if the argument of the method
tableChanged has certain property set to certain value.

Could somebody please explain or point me to documentation where I could find
such trick described?

Thank you in advance!

--

-- 
Eugene N Dzhurinsky
Nat Pryce | 2 Nov 2007 13:56
Picon

Re: verify properties of argument objects passed to methods of a mocked interface

Have a look at the documentation for parameter matchers:

http://www.jmock.org/matchers.html

and

http://www.jmock.org/custom-matchers.html

Also look at the Javadoc for org.hamcrest.Matchers.

--Nat

On 02/11/2007, Eugeny N Dzhurinsky <bofh@...> wrote:
> Hello!
>
> I would like to test the implementation of my custom TableModel (Swing). In
> this model it is possible to fire events when something was changed in the
> table. At this point TableModelListener interface is used, in particular -
> it's method tableChanged which accepts TableModelEvent.
>
> I am creating the mock for the TableModelListener and register it in
> TableModel, then I would like to test if the argument of the method
> tableChanged has certain property set to certain value.
>
> Could somebody please explain or point me to documentation where I could find
> such trick described?
>
> Thank you in advance!
>
> --
(Continue reading)

Dale King | 2 Nov 2007 14:08
Picon

Re: verify properties of argument objects passed to methods of a mocked interface

In particular if all you need to verify are things that are available via bean properties (i.e. get/is methods) then look at the org.hamcrest.Matchers.hasProperty methods.

http://jmock.org/javadoc/2.4.0/org/hamcrest/Matchers.html#hasProperty(java.lang.String)

On 11/2/07, Nat Pryce < nat.pryce-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Have a look at the documentation for parameter matchers:

http://www.jmock.org/matchers.html

and

http://www.jmock.org/custom-matchers.html

Also look at the Javadoc for org.hamcrest.Matchers.

--Nat

On 02/11/2007, Eugeny N Dzhurinsky <bofh-H63iM2dv7CBBDgjK7y7TUQ@public.gmane.org> wrote:
> Hello!
>
> I would like to test the implementation of my custom TableModel (Swing). In
> this model it is possible to fire events when something was changed in the
> table. At this point TableModelListener interface is used, in particular -
> it's method tableChanged which accepts TableModelEvent.
>
> I am creating the mock for the TableModelListener and register it in
> TableModel, then I would like to test if the argument of the method
> tableChanged has certain property set to certain value.
>
> Could somebody please explain or point me to documentation where I could find
> such trick described?
>
> Thank you in advance!
>
> --
> Eugene N Dzhurinsky
>
>

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

    http://xircles.codehaus.org/manage_email




--
Dale King
Eugene Dzhurinsky | 2 Nov 2007 14:12

Re: verify properties of argument objects passed to methods of a mocked interface

On Fri, Nov 02, 2007 at 12:56:24PM +0000, Nat Pryce wrote:
> Have a look at the documentation for parameter matchers:
> 
> http://www.jmock.org/matchers.html
> 
> and
> 
> http://www.jmock.org/custom-matchers.html
> 
> Also look at the Javadoc for org.hamcrest.Matchers.

Thank you, I've already looked at matchers, but didn't find any which would
suit my needs, so it seems I would need to create my own matcher, however I am
really confused why there's no implementation of such simple task. Probably I
am still missing something?

--

-- 
Eugene N Dzhurinsky
Dale King | 2 Nov 2007 14:17
Picon

Re: verify properties of argument objects passed to methods of a mocked interface

You said you wanted to verify a property on the passed object. Why doesn't org.hamcrest.Matchers.hasProperty do the trick?

On 11/2/07, Eugene Dzhurinsky <bofh-H63iM2dv7CBBDgjK7y7TUQ@public.gmane.org> wrote:
On Fri, Nov 02, 2007 at 12:56:24PM +0000, Nat Pryce wrote:
> Have a look at the documentation for parameter matchers:
>
> http://www.jmock.org/matchers.html
>
> and
>
> http://www.jmock.org/custom-matchers.html
>
> Also look at the Javadoc for org.hamcrest.Matchers.

Thank you, I've already looked at matchers, but didn't find any which would
suit my needs, so it seems I would need to create my own matcher, however I am
really confused why there's no implementation of such simple task. Probably I
am still missing something?

--
Eugene N Dzhurinsky




--
Dale King
Eugene Dzhurinsky | 2 Nov 2007 14:28

Re: verify properties of argument objects passed to methods of a mocked interface

On Fri, Nov 02, 2007 at 09:17:54AM -0400, Dale King wrote:
>    You said you wanted to verify a property on the passed object. Why
>    doesn't org.hamcrest.Matchers.hasProperty do the trick?

I think that would do the trick, thank you. That what I meant when was talking
about stuff I missed :)

--

-- 
Eugene N Dzhurinsky
Dale King | 2 Nov 2007 14:31
Picon

Re: verify properties of argument objects passed to methods of a mocked interface

A lot of power in JMock comes from Hamcrest, so you need to look there and not just in the org.jmock classes.

On 11/2/07, Eugene Dzhurinsky < bofh-H63iM2dv7CBBDgjK7y7TUQ@public.gmane.org> wrote:
On Fri, Nov 02, 2007 at 09:17:54AM -0400, Dale King wrote:
>    You said you wanted to verify a property on the passed object. Why
>    doesn't org.hamcrest.Matchers.hasProperty do the trick?

I think that would do the trick, thank you. That what I meant when was talking
about stuff I missed :)

--
Eugene N Dzhurinsky




--
Dale King
Junior | 7 Nov 2007 23:26
Picon
Favicon

Newbie - Returning value problem

Hi There,

I am newbie in JMock and I had problem to make a simple test returning a java.util.List.

I was following the link http://www.jmock.org/returning.html, but I hadn't success.

Below following my code, it is just a test.

Firstly, I have my interface
public interface DAO {   
    public List<String> getList();
}

Also, I have a concrete class which uses this DAO.
public class Principal {
    private DAO dao;   
    public List<String> getList() {
        return dao.getList();
    }
    //--Getters and Setters for DAO object

I would like to test the getList() method above. It is really a simple object. It simply gets the values from DAO and return it.

Now, following my TestCase

<at> RunWith(JMock.class)
public class TestPrincipal {
    Mockery context = new JUnit4Mockery();
   
    <at> Test
    public void testSoma() {
        final DAO dao = context.mock(DAO.class);               
       
        Principal p = new Principal();
        p.setDao(dao);

        final List<String> list = new ArrayList<String>();
        list.add("junior");

        context.checking(new Expectations() {{
          &nbs p; one(dao).getList();
            will(returnIterator(list));
        }});
       
        assertThat(p.getList(), equalTo(list));
    }

}


As you can see above, I am using JUnit 4 and JMock 2.

When I try to run the TestCase, I got the following exception:


java.lang.IllegalStateException: tried to return an incompatible value: expected a java.util.List but returned a java.util.AbstractList$Itr
    at org.jmock.api.Invocation.reportTypeError(Invocation.java:151)
    at org.jmock.api.Invocation.checkReturnTypeCompatibility(Invocation.java:119)
    at org.jmock.internal.InvocationExpectation.invoke(InvocationExpectation.java:127)
    at org.jmock.internal.InvocationDispatcher.dispatch(InvocationDis patcher.java:52)
    at org.jmock.Mockery.dispatch(Mockery.java:204)
    at org.jmock.Mockery.access$000(Mockery.java:37)
    at org.jmock.Mockery$MockObject.invoke(Mockery.java:246)
    at org.jmock.internal.InvocationDiverter.invoke(InvocationDiverter.java:27)
    at org.jmock.internal.ProxiedObjectIdentity.invoke(ProxiedObjectIdentity.java:36)
    at org.jmock.lib.JavaReflectionImposteriser$1.invoke(JavaReflectionImposteriser.java:33)
    at $Proxy2.getList(Unknown Source)
    at jmocktest.Principal.getList(Principal.java:14)
    at jmocktest.TestPrincipal.testSoma(TestPrincipal.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at sun.refle ct.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:615)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
    at org.jmock.integration.junit4.JMock$1.invoke(JMock.java:36)
    at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
    at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
    at org.junit.inter nal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
    at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
    at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTe sts(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Does anybody can help me please?

I appreciate any help

Best Regards
Junior

Abra sua conta no Yahoo! Mail, o único sem limite de espaço para armazenamento!
Tom Lanning | 8 Nov 2007 00:46
Picon

Re: Newbie - Returning value problem

  You are returning an iterator over the list instead of a list.  Try something like,

  context.checking(new Expectations() {{
    one(dao).getList(); will(returnValue(list));
  }});

        context.checking(new Expectations() {{
            one(dao).getList();
            will(returnIterator(list));
        }});

On Nov 7, 2007, at 5:26 PM, Junior wrote:

Hi There,

I am newbie in JMock and I had problem to make a simple test returning a java.util.List.

I was following the link http://www.jmock.org/returning.html, but I hadn't success.

Below following my code, it is just a test.

Firstly, I have my interface
public interface DAO {   
    public List<String> getList();
}

Also, I have a concrete class which uses this DAO.
public class Principal {
    private DAO dao;   
    public List<String> getList() {
        return dao.getList();
    }
    //--Getters and Setters for DAO object

I would like to test the getList() method above. It is really a simple object. It simply gets the values from DAO and return it.

Now, following my TestCase

<at> RunWith(JMock.class)
public class TestPrincipal {
    Mockery context = new JUnit4Mockery();
   
    <at> Test
    public void testSoma() {
        final DAO dao = context.mock(DAO.class);               
       
        Principal p = new Principal();
        p.setDao(dao);

        final List<String> list = new ArrayList<String>();
        list.add("junior");

        context.checking(new Expectations() {{
            one(dao).getList();
            will(returnIterator(list));
        }});
       
        assertThat(p.getList(), equalTo(list));
    }

}


As you can see above, I am using JUnit 4 and JMock 2.

When I try to run the TestCase, I got the following exception:


java.lang.IllegalStateException: tried to return an incompatible value: expected a java.util.List but returned a java.util.AbstractList$Itr
    at org.jmock.api.Invocation.reportTypeError(Invocation.java:151)
    at org.jmock.api.Invocation.checkReturnTypeCompatibility(Invocation.java:119)
    at org.jmock.internal.InvocationExpectation.invoke(InvocationExpectation.java:127)
    at org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:52)
    at org.jmock.Mockery.dispatch(Mockery.java:204)
    at org.jmock.Mockery.access$000(Mockery.java:37)
    at org.jmock.Mockery$MockObject.invoke(Mockery.java:246)
    at org.jmock.internal.InvocationDiverter.invoke(InvocationDiverter.java:27)
    at org.jmock.internal.ProxiedObjectIdentity.invoke(ProxiedObjectIdentity.java:36)
    at org.jmock.lib.JavaReflectionImposteriser$1.invoke(JavaReflectionImposteriser.java:33)
    at $Proxy2.getList(Unknown Source)
    at jmocktest.Principal.getList(Principal.java:14)
    at jmocktest.TestPrincipal.testSoma(TestPrincipal.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:615)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
    at org.jmock.integration.junit4.JMock$1.invoke(JMock.java:36)
    at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
    at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
    at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
    at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Does anybody can help me please?

I appreciate any help

Best Regards
Junior

Abra sua conta no Yahoo! Mail, o único sem limite de espaço para armazenamento!

Nat Pryce | 8 Nov 2007 09:03
Picon

Re: Newbie - Returning value problem

On 07/11/2007, Junior <jrjuniorsp@...> wrote:
> java.lang.IllegalStateException: tried to return an incompatible value:
> expected a java.util.List but returned a java.util.AbstractList$Itr

That error message could be a bit clearer.  I've raised a JIRA issue
and will fix it in the next release.

--Nat.

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

    http://xircles.codehaus.org/manage_email


Gmane