LoginSignup
12
4

More than 5 years have passed since last update.

Mockitoでorg.mockito.exceptions.misusing.InvalidUseOfMatchersException

Posted at

Mockitoを使ったテストで

when(hoge.fuga("aaa",0)).thenReturn(1)

みたいなことを書きたいとき。
第二引数の0は何が入っても同じ返り値使いたいからorg.mockito.MatchersanyInt()を使いたいのでこう直すとする

when(hoge.fuga("aaa",anyInt())).thenReturn(1)

すると

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
5 matchers expected, 4 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"));

エラーで怒られる。

when(hoge.fuga(eq("aaa"),anyInt())).thenReturn(1)

これにすると解決。

anyInt()などのmatcherを使いたいときはすべての引数をmatcherのメソッドで書く必要があるみたい。

中身がどうなってるのかさっぱりなんだけどなんでこうなるか知りたい。。。

12
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
4