java - invalid matchers when mocking -
i want test method :
public void some_method(somefactory somefactory) { a = somefactory.populatewithparameter(parameter1, parameter2, parameter3, parameter4); a.method_call(); .... }
the factory goes way
public class somefactory(){ // constructor public populatewithparameter(someclass1 someclass1, someclass2 someclass2, string string, int parameter){ return new a(someclass1, someclass2, string, parameter) } }
and test is
public void testsomemethod() throws exception { somefactory somefactory = mock(somefactory.class); when(somefactory.populatewithparameter( any(someclass1.class), any(someclass2.class), anystring(), anyint())).thenreturn(notnull()); mainactivity.some_method(somefactory); ... }
i message
org.mockito.exceptions.misusing.invaliduseofmatchersexception: invalid use of argument matchers! 4 matchers expected, 1 recorded:
you aren't allowed use notnull()
return value. mockito matchers stand in arguments in calls when
, verify
, , cannot work return values. in particular, notnull()
return null , mark "not null" match side-effect on hidden stack, lingers until interact mock next (when invoke some_method
).
though didn't list stack trace invaliduseofmatchersexception
, i'll bet error happens when invoke populatewithparameter
via some_method
, not when stub populatewithparameter
. "1 recorded" matcher notnull()
, "4 matchers expected" refer number of arguments in method call. error message tailored case forget use matcher argument, populatewithparameter(any(), any(), anystring(), 42)
, common mistake.
though see in comments "it not work!" when try return instance, can guarantee returning notnull()
absolutely cause problem, whereas returning instance may reveal different problem. may want update question full stack trace after switching returning instance, or ask new question.
for more information mockito matchers behind scenes, see my question/answer here.
Comments
Post a Comment