<bdo id='Dc9AJ'></bdo><ul id='Dc9AJ'></ul>
    <tfoot id='Dc9AJ'></tfoot>
    <i id='Dc9AJ'><tr id='Dc9AJ'><dt id='Dc9AJ'><q id='Dc9AJ'><span id='Dc9AJ'><b id='Dc9AJ'><form id='Dc9AJ'><ins id='Dc9AJ'></ins><ul id='Dc9AJ'></ul><sub id='Dc9AJ'></sub></form><legend id='Dc9AJ'></legend><bdo id='Dc9AJ'><pre id='Dc9AJ'><center id='Dc9AJ'></center></pre></bdo></b><th id='Dc9AJ'></th></span></q></dt></tr></i><div id='Dc9AJ'><tfoot id='Dc9AJ'></tfoot><dl id='Dc9AJ'><fieldset id='Dc9AJ'></fieldset></dl></div>

        <small id='Dc9AJ'></small><noframes id='Dc9AJ'>

        <legend id='Dc9AJ'><style id='Dc9AJ'><dir id='Dc9AJ'><q id='Dc9AJ'></q></dir></style></legend>
      1. Mockito 如何捕获传递给注入的模拟对象方法的参数

        时间:2023-09-25
          <bdo id='YX2IC'></bdo><ul id='YX2IC'></ul>
        • <legend id='YX2IC'><style id='YX2IC'><dir id='YX2IC'><q id='YX2IC'></q></dir></style></legend>

              <tbody id='YX2IC'></tbody>
              <i id='YX2IC'><tr id='YX2IC'><dt id='YX2IC'><q id='YX2IC'><span id='YX2IC'><b id='YX2IC'><form id='YX2IC'><ins id='YX2IC'></ins><ul id='YX2IC'></ul><sub id='YX2IC'></sub></form><legend id='YX2IC'></legend><bdo id='YX2IC'><pre id='YX2IC'><center id='YX2IC'></center></pre></bdo></b><th id='YX2IC'></th></span></q></dt></tr></i><div id='YX2IC'><tfoot id='YX2IC'></tfoot><dl id='YX2IC'><fieldset id='YX2IC'></fieldset></dl></div>
            • <small id='YX2IC'></small><noframes id='YX2IC'>

              <tfoot id='YX2IC'></tfoot>
                  本文介绍了Mockito 如何捕获传递给注入的模拟对象方法的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试测试一个内部使用 Spring AMQP 连接对象的服务类.这个连接对象是由 Spring 注入的.但是,我不希望我的单元测试实际与 AMQP 代理通信,所以我使用 Mockito 注入连接对象的模拟.

                  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 unit test to actually communicate with the AMQP broker, so I am using Mockito inject a mock of the connection object.

                  /** 
                   * The real service class being tested.  Has an injected dependency. 
                   */ 
                  public class UserService {
                  
                     @Autowired
                     private AmqpTemplate amqpTemplate;
                  
                     public final String doSomething(final String inputString) {
                        final String requestId = UUID.randomUUID().toString();
                        final Message message = ...;
                        amqpTemplate.send(requestId, message);
                        return requestId;
                     }
                  }
                  
                  /** 
                   * Unit test 
                   */
                  public class UserServiceTest {
                  
                     /** This is the class whose real code I want to test */
                     @InjectMocks
                     private UserService userService;
                  
                     /** This is a dependency of the real class, that I wish to override with a mock */
                     @Mock
                     private AmqpTemplate amqpTemplateMock;
                  
                     @Before
                     public void initMocks() {
                        MockitoAnnotations.initMocks(this);
                     }
                  
                     @Test
                     public void testDoSomething() {
                        doNothing().when(amqpTemplateMock).send(anyString(), any(Message.class));
                  
                        // Call the real service class method, which internally will make 
                        // use of the mock (I've verified that this works right).
                        userService.doSomething(...);
                  
                        // Okay, now I need to verify that UUID string returned by 
                        // "userService.doSomething(...) matches the argument that method 
                        // internally passed to "amqpTemplateMock.send(...)".  Up here 
                        // at the unit test level, how can I capture the arguments passed 
                        // to that inject mock for comparison?
                        //
                        // Since the value being compared is a UUID string created 
                        // internally within "userService", I cannot just verify against 
                        // a fixed expected value.  The UUID will by definition always be
                        // unique.
                     }
                  }
                  

                  此代码示例中的注释有望清楚地说明问题.当 Mockito 将模拟依赖注入到真实类中,并且对真实类的单元测试导致它调用模拟时,您以后如何检索传递给注入模拟的确切参数?

                  The comments in this code sample hopefully lay out the question clearly. When Mockito injects a mock dependency into a real class, and unit tests on the real class cause it to make calls to the mock, how can you later retrieve the exact arguments that were passed to the injected mock?

                  推荐答案

                  使用一个或多个 ArgumentCaptors.

                  目前尚不清楚您的类型在这里,但无论如何.假设你有一个 mock,它有一个方法 doSomething()Foo 作为参数,然后你这样做:

                  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<Foo> captor = ArgumentCaptor.forClass(Foo.class);
                  
                  verify(mock).doSomething(captor.capture());
                  
                  final Foo argument = captor.getValue();
                  
                  // Test the argument
                  

                  此外,您的方法似乎返回 void 并且您不希望它执行任何操作.就这样写吧:

                  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());
                  

                  这篇关于Mockito 如何捕获传递给注入的模拟对象方法的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:模拟中的弹簧值注入 下一篇:在同一个对象上多次调用 Mockito.when?

                  相关文章

                  最新文章

                    • <bdo id='uzocR'></bdo><ul id='uzocR'></ul>

                  1. <small id='uzocR'></small><noframes id='uzocR'>

                      <tfoot id='uzocR'></tfoot>
                    1. <i id='uzocR'><tr id='uzocR'><dt id='uzocR'><q id='uzocR'><span id='uzocR'><b id='uzocR'><form id='uzocR'><ins id='uzocR'></ins><ul id='uzocR'></ul><sub id='uzocR'></sub></form><legend id='uzocR'></legend><bdo id='uzocR'><pre id='uzocR'><center id='uzocR'></center></pre></bdo></b><th id='uzocR'></th></span></q></dt></tr></i><div id='uzocR'><tfoot id='uzocR'></tfoot><dl id='uzocR'><fieldset id='uzocR'></fieldset></dl></div>
                      <legend id='uzocR'><style id='uzocR'><dir id='uzocR'><q id='uzocR'></q></dir></style></legend>