Cannot stub method returning primitive.

2,724 views
Skip to first unread message

baradjiibrahima

unread,
Oct 6, 2009, 8:47:58 AM10/6/09
to mockito
Hi,

I'm using mockito 1.8, with jdk 1.4. I have an error when trying to
stub a method that return a boolean. The eclipse IDE tell me that the
when method take an object as argument.

Why it is not possible to stub method returning primitive ?

Thanks

Patricio Arvizu

unread,
Oct 6, 2009, 11:57:18 AM10/6/09
to mockito
Can you paste some of your code here?

Also, check this other message thread, it might help since you're on
JDK 1.4...

baradjiibrahima

unread,
Oct 6, 2009, 12:36:16 PM10/6/09
to mockito
Hi,

Here is the code:

Mockito.when(someClass.query()).thenReturn(false); // error saying
that when method accept Object argument but not
//
boolean argument since someClass.query() return boolean
But I tried this:

Mockito.when(Boolean.valueOf(someClass.query())).thenReturn
(Boolean.valueOf(false));

and it seems to work.

In your last post you told me to see a message thread related to 1.4.
Can you give me the link ?
I notice that I cannot run Mockito on jre 1.4.

Thanks
> > Thanks- Masquer le texte des messages précédents -
>
> - Afficher le texte des messages précédents -

Karl Schaefer

unread,
Oct 6, 2009, 12:42:13 PM10/6/09
to moc...@googlegroups.com
There is no autoboxing/autounboxing of primitives to wrapper types
until 1.5. You will need to manually box/unbox your primitives.

baradjiibrahima

unread,
Oct 6, 2009, 12:49:19 PM10/6/09
to mockito
Thanks for the response. Can I resonably use Mockito with jdk 1.4 ? Is
it working ?
> >> - Afficher le texte des messages précédents -- Masquer le texte des messages précédents -

szczepiq

unread,
Oct 7, 2009, 6:17:37 AM10/7/09
to moc...@googlegroups.com
>Can I resonably use Mockito with jdk 1.4 ?

Mockito doesn't work with jdk 1.4 because it's compiled for 1.5. Some
java5 features are essential for Mockito mocking syntax.

However, one of the user came up with very interesting idea: given you
don't want to upgrade your production code to java 1.4 for whatever
reason, you can at least upgrade your *test code* to 1.5 and compile
your test code with 1.5 compatibility. Never tried it but in theory it
should work: your prod classes compiled with 1.4, test code with 1.5.
Check out the other thread related to 1.4 on this mailing list.

Good luck!

Cheers,
Szczepan Faber

Michael Hackett

unread,
Oct 7, 2009, 11:35:51 AM10/7/09
to moc...@googlegroups.com
This is what we are doing for a J2ME app we are working on. The application is compiled for Java 1.1 and with the J2ME libraries, while the test code is Java 1.5 with all of its libraries (plus MockME to fill out the J2ME bits that are missing from SE). Works quite well!

We had to split it up into two modules/projects to keep Maven and Eclipse happy (it's possible to pull it off with Maven, but it's ugly; but I found no way to have Eclipse treat different parts of one project with different Java settings). It could probably be done fairly easily with Ant, if you weren't using the IDE's compiler. But splitting into two projects is quite straightforward.

2009/10/7 szczepiq <szcz...@gmail.com>

szczepiq

unread,
Oct 7, 2009, 2:12:51 PM10/7/09
to moc...@googlegroups.com
>We had to split it up into two modules/projects to keep Maven and Eclipse happy

Right, I suspected you had to find some workaround for the IDE.
Splitting tests and code into separate Eclipse projects - nice idea!

Cheers,
Szczepan Faber
Reply all
Reply to author
Forward
0 new messages