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

      <bdo id='iX34S'></bdo><ul id='iX34S'></ul>

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

    2. <legend id='iX34S'><style id='iX34S'><dir id='iX34S'><q id='iX34S'></q></dir></style></legend>

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

      时间:2023-10-01
        1. <tfoot id='bCDpW'></tfoot>

        2. <legend id='bCDpW'><style id='bCDpW'><dir id='bCDpW'><q id='bCDpW'></q></dir></style></legend>

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

              <bdo id='bCDpW'></bdo><ul id='bCDpW'></ul>

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

                  <tbody id='bCDpW'></tbody>
                本文介绍了修改 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 函数的输入参数,然后读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:mockito 的学习资源 下一篇:为什么我无法在使用 Maven 的 Junit 测试运行中访问

                相关文章

                最新文章

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

                  1. <legend id='06iGZ'><style id='06iGZ'><dir id='06iGZ'><q id='06iGZ'></q></dir></style></legend>

                    <small id='06iGZ'></small><noframes id='06iGZ'>

                  2. <tfoot id='06iGZ'></tfoot>