Java Mailing List Archive

http://www.gg3721.com/

Home » user.jmock »

[jmock-user] Get hold of parameter to mocked object

Mattias Jiderhamn

2009-08-06

Replies: Find Java Web Hosting

Author LoginPost Reply
What is the recommended practice for getting hold of the parameter
passed to a mock object, for further testing.

An example of what I want to do:

OrderDAO orderDAO = context.mock(OrderDAO.class);

checking(new Expectations() {
   {
    oneOf(orderDAO).save(with(any(Order.class)));
   }
  });

doSomethingThatCreatesAndSavesOrder();

Order order = ...; // The order passed to the mocked OrderDAO.save()

// Regular jUnit tests
assertEquals("1", order.getCustomer());
assertEquals(3, order.getNoOfLines());
...

I know I can do this using custom matchers:

...
oneOf(orderDAO).save(with(allOf(
   any(Order.class),
   Matchers.hasProperty("customer",
org.hamcrest.CoreMatchers.equalTo("1") // (Need to be declared as local
variable for generics to work)
)));
...

but if the test fails, the error does not include the specific test that
failed but only an "unexpected invocation".

The solution that I have come up with would be to create a Matcher that
remembers the object it is asked to match, which could then be accessed
for further testing.
Seems a bit ugly though. My gut tells me somebody should have thought
about this already.
Is there a better way???

--

</Mattias>


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

  http://xircles.codehaus.org/manage_email


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