junit - Mockito - You cannot use argument matchers outside of verification or stubbing - have tried many things but still no solution -


i have following piece of code:

powermockito.mockstatic(dateutils.class);       //and line exception - notice it's static function   powermockito.when(dateutils.isequalbydatetime (any(date.class),any(date.class)).thenreturn(false); 

the class begins with:
@runwith(powermockrunner.class) @preparefortest({cm9dateutils.class,dateutils.class})

and org.mockito.exceptions.invaliduseofmatchersexception...... cannot use argument matchers outside of verification or stubbing..... (the error appears twice in failure trace - both point same line)

in other places in code usage of when done , it's working properly. also, when debugging code found any(date.class) returns null.

i have tried following solutions saw other people found useful, me doesn't work:

  1. adding @after public void checkmockito() { mockito.validatemockitousage(); }
    or
    @runwith(mockitojunitrunner.class)
    or
    @runwith(powermockrunner.class)

  2. change powermockito.when(new boolean(dateutils.isequalbydatetime(any(date.class), any(date.class)))).thenreturn(false);

  3. using anyobject() (it doesn't compile)

  4. using notnull(date.class) or (date)notnull()

  5. replace when(........).thenreturn(false);

    with
    boolean falsebool=new boolean(false);
    and
    when(.......).thenreturn(falsebool);

as detailed on powermockito getting started page, you'll need use both powermock runner @preparefortest annotation.

@runwith(powermockrunner.class) @preparefortest(dateutils.class) 

ensure you're using @runwith annotation comes junit 4, org.junit.runner.runwith. because it's accepted value attribute, it's pretty odd me you're receiving error if you're using correct runwith type.

any(date.class) correct return null: rather using magic "matches date" instance, mockito uses a hidden stack of matchers track matcher expressions correspond matched arguments, , returns null objects (and 0 integers, , false booleans, , forth).


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -