0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[リサーチ・メモ]「when-thenReturn」と「doReturn-when」の違い

Last updated at Posted at 2023-07-15

背景

何気なく使用していた「when-thenReturn」と「doReturn-when」の違いが気になり調査したが、挙動は変わらないという記事ばかりだった。しかし、仕事で実際は挙動が違うことを教えて頂いた為、備忘録として纏める。

when-thenReturn

対象のメソッドを実際に呼び出し、戻り値を差し替える。

doReturn-when

対象のメソッドを呼び出さないで、戻り値だけ返す。

まとめ

ルート確認、呼び出し回数の確認が必要な場合は、when-thenReturnを使用する。
呼び出せない、または呼び出しをスキップしたい場合は、doReturn-whenを使用する。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?