"'void' method not allowed here" when using matchers

1,552 views
Skip to first unread message

Marc Ende

unread,
Mar 17, 2010, 6:13:59 AM3/17/10
to mockito
Hi,

there is something what I don't understand:
I've got a mock-object:

private Session session;

which I initialize in the setUp-method:

session = mock(Session)

It's an Hibernate Session (org.hibernate.Session)
When I try to set a "when(...)" with matchers I'll receive an error
"'void' method not allowed here":

when(session.load(anyObject(),anyString())).thenReturn(a)

the same happens when I change the Matchers to:

when(session.load(any(A.class),eq("123"))).thenReturn(a);

I had a look on anyObject(), anyString() etc. they're all "non-void-
methods".
Also the session.load(...,...) isn't a "void-method".

Everything works great when I remove the matchers:
when(session.load(A.class, "123").thenReturn(a)

It's a little bit strange. Does anybody has an idea?

I'm using netbeans and fest-asserts.

yours

Marc

szczepiq

unread,
Mar 17, 2010, 8:38:02 AM3/17/10
to moc...@googlegroups.com
Hmmmm, not sure.

I created an issue: http://code.google.com/p/mockito/issues/detail?id=180

Can you comment on the issue and specify what Mockito version you use?
Cheers,
Szczepan


--
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.


szczepiq

unread,
Mar 17, 2010, 8:59:24 AM3/17/10
to moc...@googlegroups.com
I think I know what is the problem.

session.load() is overloaded big time. When you use matchers, compiler binds the call with a 'void' variant of load() method. Solution:

1. cast the parameters correctly so that compiler picks up the right method.
2. update version of Mockito. Current mockito gives better message when this problem occurs.

Cheers,
Szczepan

Marc Ende

unread,
Mar 17, 2010, 9:02:19 AM3/17/10
to moc...@googlegroups.com
I'm not sure if that's the solution. I had the same problem on another project. There haven't been
any hibernate-jar's or sessions.
I'll search for this project this evening. If I find the place I'll try to build a test-project which I will add to the
issue.

cheers

marc

szczepiq

unread,
Mar 17, 2010, 9:09:35 AM3/17/10
to moc...@googlegroups.com
Marc,

This is not related to Hibernate. It is related to mocking type that has bunch of overloaded methods & how java compiler works :)

when(session.load(anyObject(),anyString())).thenReturn(a)

has different meaning than:

when(session.load((Class) anyObject(),anyString())).thenReturn(a)

Have a look at the Session.load method variants:

https://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#load%28java.lang.Class,%20java.io.Serializable%29

Cheers,
Szczepan

Marc Ende

unread,
Mar 17, 2010, 9:19:13 AM3/17/10
to moc...@googlegroups.com
Ah okay, I see what you mean. I haven't thought about the other overloaded functions.
You're totally right. I had tested it the way you've proposed and it work perfectly now.

The error-message is a little bit confusing....

Thanks for your help, I think you can close the issue.

Marc
Reply all
Reply to author
Forward
0 new messages