2017-11-30 17 views
0

を使用して関数の引数は、私がはArgumentCaptorのmockito

Somefun(){ 

    mockedService.execute(()->{ 
     //function body 
    }) 

} 

を持っているキャプチャするためにどのように私はモック内部のメソッドを実行実行したいです。どうすればいいですか? 私はこのモック(関数である)の引数を取り込み、それを実行すると、何らかの形で私の仕事が完了すると考えました。これを達成する方法はありますか?または他の方法で。 ありがとう!

答えて

2

それはこのようなものになります。

@RunWith(MockitoRunner.class) 
public class SomeFunTest { 
    @Mock Service mockedService; 
    @Captor ArgumentCaptor<Runnable /* or whatever type of function you expect there*/> functionCaptor; 

    @Test 
    public void serviceShouldInvokeFunction() { 
     // given 
     ObjectUnderTest out = new ObjectUnderTest(mockedService); 

     // when 
     out.SomeFun(); 

     // then 
     verify(mockedService).execute(captor.capture()); 

     /* Do your assertions after you captured the value */ 
     assertThat(captor.getValue()).isInstanceOf(Runnable.class); 
    } 
} 
+0

クールを、何とか私は完全に今まで@Captorを逃しました。素晴らしいヒント、ありがとう。 –