Matcher for List inside of bean?

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

Matcher for List inside of bean?

by Keith Heasman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Good day.  I've just started using jMock after realizing EasyMock does not do it
for us.  Great lib so far.

Does the 'equal' matcher work to match elements of a List inside of a bean?  For
instance, here's my expectations snippet:

final JobBean bean = new JobBean();
bean.setId("A");
bean.addIdToList(1);

context.checking(new Expectations() {{
  one(senderMock).sendMessage(with(equal(bean)));
}});

However, I get:

unexpected invocation
unexpected invocation: sender.sendMessage(<xxx.JobBean@caf083[id=A,idList=[i]]>)

The JobBean is simple:

public class JobBean {
  private List<Integer> idList;

  public addIdToList(Integer id) {
    if (idList == null) {
      idList = new Vector<Integer>();
    }
    idList.add(id);
  }

  public List<Integer> getIdList() {
     return idList;
  }

...

I can write a custom Matcher but I want to be sure I'm using the jMock
functionality correctly.

thanks in advance.

k.



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

    http://xircles.codehaus.org/manage_email



Re: Matcher for List inside of bean?

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Glad to hear it :) Perhaps you could post something to the world about  
what made the difference?

- -
The equal() clause uses the equals() method on the expected object,  
although it will compare arrays too. So, you need to have an equals  
method on your bean. Alternatively, if you just want an individual  
field you can use

hasProperty("idList", equalTo(<Expected List>))

or something like that.

We ought to add a reflective equals for this kind of thing.

- -
For a quick fix you could use the apache commons lang EqualsBuilder  
either for quick customer matcher or in the bean

public static Matcher<JobBean> equalTo(final JobBean expected) {
   return new TypeSafeMatcher<JobBean>() {
     public boolean matches(JobBean actual) {
       return EqualsBuilder.reflectionEquals(expected, actual);
     }
     // ...
}

S.

On 6 Jun 2008, at 16:50, kheasman wrote:

> Good day.  I've just started using jMock after realizing EasyMock  
> does not do it
> for us.  Great lib so far.
>
> Does the 'equal' matcher work to match elements of a List inside of  
> a bean?  For
> instance, here's my expectations snippet:
>
> final JobBean bean = new JobBean();
> bean.setId("A");
> bean.addIdToList(1);
>
> context.checking(new Expectations() {{
>  one(senderMock).sendMessage(with(equal(bean)));
> }});
>
> However, I get:
>
> unexpected invocation
> unexpected invocation:  
> sender.sendMessage(<xxx.JobBean@caf083[id=A,idList=[i]]>)
>
> The JobBean is simple:
>
> public class JobBean {
>  private List<Integer> idList;
>
>  public addIdToList(Integer id) {
>    if (idList == null) {
>      idList = new Vector<Integer>();
>    }
>    idList.add(id);
>  }
>
>  public List<Integer> getIdList() {
>     return idList;
>  }
>
> ...
>
> I can write a custom Matcher but I want to be sure I'm using the jMock
> functionality correctly.
>
> thanks in advance.
>
> k.
>
>

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


LightInTheBox - Buy quality products at wholesale price!