【问题标题】:How to capture arguments which are functions using ArgumentCaptor mockito如何使用 ArgumentCaptor mockito 捕获作为函数的参数
【发布时间】:2017-11-30 04:28:16
【问题描述】:

我有

Somefun(){

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

}

所以我想在模拟中运行执行方法。我该怎么做? 我发现如果我以某种方式捕获这个模拟(这是一个函数)的参数并执行它,我的工作就会完成。有没有办法实现这一目标?或者其他方式。 谢谢!

【问题讨论】:

标签: java mockito testng


【解决方案1】:

看起来像这样:

@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); 
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-02
    • 1970-01-01
    • 2011-04-06
    • 2016-12-04
    相关资源
    最近更新 更多