goldfrapp04
goldfrapp04

Reputation: 2356

Mockito: How to get the arguments passed to a method when the return type of the method is void

I've read this post, but my problem is that my myFunction returns void instead of an Object. So I get errors at

when(mock.myFunction(anyString()))

saying

when (java.lang.Void) in Mockito cannot be applied 
to   (void)

How can I deal with this issue?

Upvotes: 8

Views: 6119

Answers (2)

geoand
geoand

Reputation: 63991

I have the same answer in a comment on the question, but just to make it easier for future readers to see, here it is.

doNothing().when(mock).myFunction(anyString());

in order to be able to handle the void return type.

Upvotes: 12

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79808

The answer to this can be found in my answer to that other post that you linked to.

doAnswer(returnsFirstArg()).when(mock).myFunction(anyString());

where the returnsFirstArg() method is static in the AdditionalAnswers class.

Upvotes: 3

Related Questions