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

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

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

      <tfoot id='a4U8M'></tfoot>

        <legend id='a4U8M'><style id='a4U8M'><dir id='a4U8M'><q id='a4U8M'></q></dir></style></legend>
      1. 如何验证具有不同参数的多个方法调用

        时间:2023-09-26
          <bdo id='tJFaE'></bdo><ul id='tJFaE'></ul>

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

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

              <tfoot id='tJFaE'></tfoot>

                  本文介绍了如何验证具有不同参数的多个方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有以下方法希望验证其行为.

                  I have the following method that I wish to verify behaviour on.

                  public void methodToTest(Exception e, ActionErrors errors) {
                      ...
                  
                      errors.add("exception.message", 
                              ActionMessageFactory.createErrorMessage(e.toString()));
                  
                      errors.add("exception.detail",
                              ActionMessageFactory.createErrorMessage(e.getStackTrace()[0].toString()));
                  
                      ...
                  }
                  

                  在我的@Test 类中,我希望做这样的事情来验证 errors.add() 是用exception.message"调用的.再次使用exception.detail"

                  In my @Test class I was hoping to do something like this to verify that errors.add() is called with "exception.message" and again with "exception.detail"

                  verify(errors).add(eq("exception.message"), any(ActionError.class));
                  verify(errors).add(eq("exception.detail"), any(ActionError.class));
                  

                  然而 Mockito 抱怨如下

                  however Mockito complains as follows

                  Argument(s) are different! Wanted:
                  actionErrors.add(
                      "exception.message",
                      <any>
                  );
                  
                  Actual invocation has different arguments:
                  actionErrors.add(
                      "exception.detail",
                      org.apache.struts.action.ActionError@38063806
                  );
                  

                  如何让 Mockito 检查这两个值?

                  How can I tell Mockito to check for both values?

                  推荐答案

                  进一步阅读使我尝试使用 ArgumentCaptors 和以下作品,尽管比我想要的要冗长得多.

                  Further reading has led me to try using ArgumentCaptors and the following works, although much more verbose than I would like.

                  ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
                  
                  verify(errors, atLeastOnce()).add(argument.capture(), any(ActionMessage.class));
                  
                  List<String> values = argument.getAllValues();
                  
                  assertTrue(values.contains("exception.message"));
                  assertTrue(values.contains("exception.detail"));
                  

                  这篇关于如何验证具有不同参数的多个方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:doThrow() doAnswer() doNothing() 和 doReturn() 在 mockito 中 下一篇:Mockito 验证不再与任何模拟交互

                  相关文章

                  最新文章

                  1. <tfoot id='nFwLS'></tfoot>
                  2. <legend id='nFwLS'><style id='nFwLS'><dir id='nFwLS'><q id='nFwLS'></q></dir></style></legend>

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

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

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