How can Mockito capture arguments passed to an injected mock object's methods?

前端 未结 2 612
悲哀的现实
悲哀的现实 2020-12-28 12:24

I am trying to test a service class, which internally makes use of a Spring AMQP connection object. This connection object is injected by Spring. However, I don\'t want my

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-28 12:55

    Use one, or more, ArgumentCaptors.

    It is unclear what your types are here, but anyway. Let's suppose you have a mock which has a method doSomething() taking a Foo as an argument, then you do this:

    final ArgumentCaptor captor = ArgumentCaptor.forClass(Foo.class);
    
    verify(mock).doSomething(captor.capture());
    
    final Foo argument = captor.getValue();
    
    // Test the argument
    

    Also, it looks like your method returns void and you don't want it to do anything. Just write this:

    doNothing().when(theMock).doSomething(any());
    

提交回复
热议问题