2015-04-27 7 views
9

voglio fare questo:Mockito Matchers.any (...) su un argomento solo

verify(function, Mockito.times(1)).doSomething(argument1, Matchers.any(Argument2.class)); 

Dove argument1 è un'istanza specfic di tipo argument1 e argument2 è un qualsiasi istanza del tipo Argument2.

Ma ottengo un errore:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 2 matchers expected, 1 recorded. This exception may occur if matchers are combined with raw values: 
    //incorrect: 
    someMethod(anyObject(), "raw String"); When using matchers, all arguments have to be provided by matchers. For example: 
    //correct: 
    someMethod(anyObject(), eq("String by matcher")); 

In seguito a tale consiglio che posso scrivere il seguente ed è tutto a posto:

verify(function, Mockito.times(1)).doSomething(Matchers.any(Argument1.class), Matchers.any(Argument2.class)); 

Dove sto cercando qualsiasi arguement di tipo argument1 e qualsiasi argomento di tipo Argument2.

Come posso ottenere questo comportamento desiderato?

risposta

13

C'è più di un possibile parametro di confronto e uno è eq, che è menzionato nel messaggio di eccezione. Usa:

verify(function, times(1)).doSomething(eq(arg1), any(Argument2.class)); 

(importazioni statiche dovuto essere lì - eq() è Matchers.eq()).

Hai anche same() (che fa riferimento all'uguaglianza, ovvero ==) e più in generale puoi scrivere i tuoi corrispondenti.

+0

Fissarmi in faccia mi serve per non aver letto il mio tempo! Grazie per quello. –

Problemi correlati