<legend id='3Huf2'><style id='3Huf2'><dir id='3Huf2'><q id='3Huf2'></q></dir></style></legend>

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

        <small id='3Huf2'></small><noframes id='3Huf2'>

      1. 修改 void 函数的输入参数,然后读取

        Modify input parameter of a void function and read it afterwards(修改 void 函数的输入参数,然后读取)

        <legend id='COpYJ'><style id='COpYJ'><dir id='COpYJ'><q id='COpYJ'></q></dir></style></legend>

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

                <tfoot id='COpYJ'></tfoot>
                  <tbody id='COpYJ'></tbody>
              1. <small id='COpYJ'></small><noframes id='COpYJ'>

                  <bdo id='COpYJ'></bdo><ul id='COpYJ'></ul>
                  本文介绍了修改 void 函数的输入参数,然后读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个相当复杂的 java 函数,我想使用 jUnit 进行测试,为此我正在使用 Mockito.这个函数看起来像这样:

                  I have a rather complex java function that I want to test using jUnit and I am using Mockito for this purpose. This function looks something like this:

                  public void myFunction (Object parameter){
                     ...
                     doStuff();
                     ...
                     convert(input,output);
                     ...
                     parameter.setInformationFrom(output);
                  }
                  

                  convert 函数根据输入设置输出的属性,它是一个 void 类型的函数,尽管输出"是参数是正在使用的,就好像它是由函数返回的一样.这个转换函数是我想要模拟的,因为我不需要依赖于测试的输入,但我不知道该怎么做,因为我对 Mockito 不是很熟悉.

                  The function convert sets the attributes of output depending on input and it's a void type function, although the "output" parameter is what is being used as if it were returned by the function. This convert function is what I want to mock up as I don't need to depend on the input for the test, but I don't know how to do this, as I am not very familiar with Mockito.

                  我见过的基本情况是 when(something).thenReturn(somethingElse)或我理解的 doAnswer 方法与前一个方法类似,但可以添加更多逻辑,但我认为这些情况不适合我的情况,因为我的函数没有返回语句.

                  I have seen basic cases as when(something).thenReturn(somethingElse) or the doAnswer method which I understand is similar to the previous one but more logic can be added to it, but I don't think these cases are appropriate for my case, as my function does not have a return statement.

                  推荐答案

                  如果您希望模拟方法在(或以其他方式更改)参数上调用方法,您需要像这个问题一样写一个答案(如何模拟影响对象的 void 返回方法").

                  If you want the mocked method to call a method on (or otherwise alter) a parameter, you'll need to write an Answer as in this question ("How to mock a void return method affecting an object").

                  来自 Kevin Welker 的 回答那里:

                  doAnswer(new Answer() {
                      Object answer(InvocationOnMock invocation) {
                          Object[] args = invocation.getArguments();
                          ((MyClass)args[0]).myClassSetMyField(NEW_VALUE);
                          return null; // void method, so return null
                      }
                  }).when(mock).someMethod();
                  

                  请注意,较新的最佳实践将具有 Answer 的类型参数,如 Answer,并且 Java 8 的 lambda 可以进一步压缩语法.例如:

                  Note that newer best-practices would have a type parameter for Answer, as in Answer<Void>, and that Java 8's lambdas can compress the syntax further. For example:

                  doAnswer(invocation -> {
                    Object[] args = invocation.getArguments();
                    ((MyClass)args[0]).myClassSetMyField(NEW_VALUE);
                    return null; // void method in a block-style lambda, so return null
                  }).when(mock).someMethod();
                  

                  这篇关于修改 void 函数的输入参数,然后读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
                  1. <i id='GrkEB'><tr id='GrkEB'><dt id='GrkEB'><q id='GrkEB'><span id='GrkEB'><b id='GrkEB'><form id='GrkEB'><ins id='GrkEB'></ins><ul id='GrkEB'></ul><sub id='GrkEB'></sub></form><legend id='GrkEB'></legend><bdo id='GrkEB'><pre id='GrkEB'><center id='GrkEB'></center></pre></bdo></b><th id='GrkEB'></th></span></q></dt></tr></i><div id='GrkEB'><tfoot id='GrkEB'></tfoot><dl id='GrkEB'><fieldset id='GrkEB'></fieldset></dl></div>

                      <legend id='GrkEB'><style id='GrkEB'><dir id='GrkEB'><q id='GrkEB'></q></dir></style></legend>
                          <tbody id='GrkEB'></tbody>

                            <bdo id='GrkEB'></bdo><ul id='GrkEB'></ul>
                            <tfoot id='GrkEB'></tfoot>

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