Rollerball
Rollerball

Reputation: 13108

How to verify that a method gets called with any argument?

I would like to verify() with Mockito whether or not a method gets called. Since I do not know the argument I would like to do that in such a way that it verifies with any() argument. Is it possible? At the moment I am getting "error, wanted x and found y". I do not care about the parameter passed, I would like just to know if the method gets called at all. Thanks in advance.

As of now I have tried:

when(userBean.getProfile().getLanguage().getValue()).thenReturn("fr");
verify((userBean), atLeastOnce()).getProfile().getLanguage().getValue();

userBean has been mocked with RETURN_DEEP_STUBS. Getting a null pointer exception though. May be due to the fact that userBean is an EJB?

Upvotes: 0

Views: 120

Answers (1)

Jeff Bowman
Jeff Bowman

Reputation: 95614

As in the Mockito documentation for RETURNS_DEEP_STUBS:

Verification only works with the last mock in the chain. You can use verification modes.

For your example:

 /* BAD */ verify(userBean, atLeastOnce()).getProfile().getLanguage().getValue();
/* GOOD */ verify(userBean.getProfile().getLanguage(), atLeastOnce()).getValue();

(Added as a separate answer to point to the documentation link.)

Upvotes: 1

Related Questions