10
9

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 5 years have passed since last update.

MockitoのArgumentCaptorでリスト型を扱いたいとき

Last updated at Posted at 2016-01-18

リスト型の引数のメソッドのテストで、わたされた引数についてassertionしたいときのメモ。
@Captorアノテーションを使うのと、MockitoAnnotations#initMocksを使うのがみそ。

手順

  • @Captorをつけて欲しいArgumentCaptorを定義
  • @BeforeでinitMocks(this)をして、captorのインスタンス生成
  • verifyでcaptor.capture()で渡された引数を指定
  • captor.getValue()で渡された引数を取得
SampleTest.java

@Captor
ArgumentCaptor<List<Person>> captor;

@Before
public void before() {
  MockitoAnnotations.initMocks(this);
}

@Test
public void test() {
  PersonService service = ... ;
  
  service.execute();
  
  
  verify(service).update(captor.capture());

  assertThat(captor.getValue()).isEmpty();
}

参考サイト

10
9
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
10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?