Can Mockito stub a method without regard to the argument?

前端 未结 4 693
离开以前
离开以前 2020-12-12 11:33

I\'m trying to test some legacy code, using Mockito.

I want to stub a FooDao that is used in production as follows:

foo = fooDao.getBar(         


        
4条回答
  •  一生所求
    2020-12-12 11:56

    when(
      fooDao.getBar(
        any(Bazoo.class)
      )
    ).thenReturn(myFoo);
    

    or (to avoid nulls):

    when(
      fooDao.getBar(
        (Bazoo)notNull()
      )
    ).thenReturn(myFoo);
    

    Don't forget to import matchers (many others are available):

    For Mockito 2.1.0 and newer:

    import static org.mockito.ArgumentMatchers.*;
    

    For older versions:

    import static org.mockito.Matchers.*;
    

提交回复
热议问题