user86834 user86834 - 3 months ago 23
Java Question

Mock Spring's MessageSource.getMessage method

I'm trying to mock Spring's

MessageSource.getMessage
method but
Mockito
it is complaining with an unhelpful message, I am using:

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


The error message is:

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

when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());

verify(mock).someMethod(contains("foo"))

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()
/hashCode().


Any idea what I am doing wrong?

Answer

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("").