Order cannot be returned by toString()

3,676 views
Skip to first unread message

Gabriel

unread,
Aug 24, 2012, 4:40:40 AM8/24/12
to moc...@googlegroups.com
Hi all,

I am mocking an object using an interface (OrderService):

private OrderService mockService = mock(OrderService.class);

When I try to use when(), I get this error:

Order cannot be returned by toString()

Here is how I set it up:

when(mockService.createOrder(inputOrder)).thenReturn(outputOrder);

I don't understand how/what this error means. The Order class uses the Apache ToStringBuilder, EqualsBuilder & HashCodeBuilder. I use the relection based methods, could it be that Mockito doesn't like that? I don't see what else it could be but I have other tests that use this and other classes that use the same builder code. I don't see why it would be calling toString() on anything either. inputOrder & outputOrder are instances of the real objects btw.

I am using Mockito 1.8.5.

Thanks,
Gabriel

Brice Dutheil

unread,
Aug 24, 2012, 4:44:03 AM8/24/12
to moc...@googlegroups.com
Hi,

The small stub line you provided looks correct, could you share more code on the context ?

-- Brice

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mockito/-/Ct0icE2ycxEJ.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.

David Wallace

unread,
Aug 24, 2012, 4:44:16 AM8/24/12
to moc...@googlegroups.com
Hi Gabriel,
Thanks for posting.

I think you'll need to show us a little more of your code before we can really help you.  Can you attach both the OrderService class and your test code to another message?

Kind regards,
David Wallace.

Gabriel Rossetti

unread,
Aug 24, 2012, 5:24:54 AM8/24/12
to moc...@googlegroups.com
Hi David & Brice,

sure, I attached shortened versions of the relevant classes.

Thanks,
Gabriel


________________________________
From: moc...@googlegroups.com [moc...@googlegroups.com] on behalf of David Wallace [dmwall...@gmail.com]
Sent: Friday, August 24, 2012 10:44 AM
To: moc...@googlegroups.com
Subject: Re: [mockito] Order cannot be returned by toString()

Hi Gabriel,
Thanks for posting.

I think you'll need to show us a little more of your code before we can really help you. Can you attach both the OrderService class and your test code to another message?

Kind regards,
David Wallace.

On Fri, Aug 24, 2012 at 8:40 PM, Gabriel <gabriel....@trafigura.com<mailto:gabriel....@trafigura.com>> wrote:
Hi all,

I am mocking an object using an interface (OrderService):

private OrderService mockService = mock(OrderService.class);

When I try to use when(), I get this error:

Order cannot be returned by toString()

Here is how I set it up:

when(mockService.createOrder(inputOrder)).thenReturn(outputOrder);

I don't understand how/what this error means. The Order class uses the Apache ToStringBuilder, EqualsBuilder & HashCodeBuilder. I use the relection based methods, could it be that Mockito doesn't like that? I don't see what else it could be but I have other tests that use this and other classes that use the same builder code. I don't see why it would be calling toString() on anything either. inputOrder & outputOrder are instances of the real objects btw.

I am using Mockito 1.8.5.

Thanks,
Gabriel


--
You received this message because you are subscribed to the Google Groups "mockito" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mockito/-/Ct0icE2ycxEJ.
To post to this group, send email to moc...@googlegroups.com<mailto:moc...@googlegroups.com>.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com<mailto:mockito%2Bunsu...@googlegroups.com>.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.


--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.

________________________________

This email and any attachments are confidential and access to this email or attachment by anyone other than the addressee is unauthorised. If you are not the intended recipient please notify the sender and delete the email including any attachments. You must not disclose or distribute any of the contents to any other person. Personal views or opinions are solely those of the author and not of Trafigura. Trafigura does not guarantee that the integrity of this communication has been maintained nor that the communication is free of viruses, interceptions or interference. By communicating with anyone at Trafigura by email, you consent to the monitoring or interception of such email by Trafigura in accordance with its internal policies. Unless otherwise stated, any pricing information given in this message is indicative only, is subject to change and does not constitute an offer to deal at any price quoted.
Order.java
OrderService.java
OrderServiceFacade.java
OrderServiceFacadeTest.java

Gabriel Rossetti

unread,
Aug 24, 2012, 5:42:20 AM8/24/12
to moc...@googlegroups.com, Gabriel Rossetti
I didn't realize that I was using a rather old version of Mockito, I upgraded to 1.9.0 and this issue went away. Sorry, I should have checked that first, I was sure I had been using the latest version for some reason.

Thanks,
Gabriel

Brice Dutheil

unread,
Aug 24, 2012, 5:43:28 AM8/24/12
to moc...@googlegroups.com, Gabriel Rossetti
OK great ;)
Hoppy mocking then.

Cheers,

-- Brice Dutheil

Reply all
Reply to author
Forward
0 new messages