user86834 user86834 - 1 year ago 157
Java Question

Mock Spring's MessageSource.getMessage method

I'm trying to mock Spring's

method but
it is complaining with an unhelpful message, I am using:

when(mockMessageSource.getMessage(anyString(), any(Object[].class), any(Locale.class)))

The error message is:

You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:

doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());


Also, this error might show up because you use argument matchers with methods
that cannot be mocked Following methods *cannot* be stubbed/verified: final/private/equals()

Any idea what I am doing wrong?

Answer Source

The problem, I believe, is that anyString() is the matcher it is complaining about when it is used as a parameter in your thenReturn(...) call. If you don't care what's returned, just return an empty String a la thenReturn("").

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download