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

        <tfoot id='sWVa1'></tfoot>

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

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

        如何模拟影响对象的无效返回方法

        时间:2023-09-25

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

                    <tbody id='Pe5iy'></tbody>

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

                  <tfoot id='Pe5iy'></tfoot>

                  本文介绍了如何模拟影响对象的无效返回方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在为我的应用程序编写单元测试,我想知道 Mockito 框架是否有可能影响传递给返回模拟类的 void 方法的对象.例如,调用一个模拟验证类,该类包含一个返回 void 但通过作为参数传入的对象跟踪各种更改和元数据的方法..

                  I am writing unit tests for my application and I was wondering if it is possible for the Mockito framework to affect an object that is passed into a method that returns void of a mocked class. For instance, calling a mocked validation class that contains a method that returns void but tracks various changes and metadata via an object passed in as an argument. .

                  public GetCartItemsOutput getCartItems(GetCartItemsInput getCartItemsInput) {
                      CartItemsFilter cartItemsFilter = new CartItemsFilter();
                      validator.validateCartItemsInput(getCartItemsInput, cartItemsFilter); ...
                  

                  我为我的其他测试模拟了验证器类,但对于这个我需要模拟对 cartItemsFilter 对象的更改,我不知道该怎么做.

                  I mocked the validator class for my other tests but for this one I need mock the changes to the cartItemsFilter object which I do not know how to do.

                  推荐答案

                  答案是肯定的,你可以,而且基本上有两个级别,根据你的测试需要.

                  The answer is yes, you can, and there are basically two levels of doing this, based on the need of your test.

                  如果您只想测试与模拟对象的交互,您可以简单地使用 verify() 方法,验证是否调用了 void 方法.

                  If you merely want to test the interaction with the mocked object, you can simply use the verify() method, to verify that the void method was called.

                  如果您的测试确实需要模拟对象来修改传递给它的参数,您将需要实现 答案:

                  If your test genuinely needs the mocked object to modify parameters passed to it, you will need to implement an Answer:

                  EDITED 显示使用 void 方法的正确形式

                  EDITED to show proper form of using Answer with void method

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

                  在 Java 8+ 中,上面的内容使用 lambda 进行了简化:

                  In Java 8+, the above is simplified with a lambda:

                  doAnswer(invocation-> {
                      Object[] args = invocation.getArguments();
                      ((MyClass)args[0]).myClassSetMyField(NEW_VALUE);
                      return null;
                  }).when(mock).someMethod();
                  

                  这篇关于如何模拟影响对象的无效返回方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何通过mockito验证是否从其他具有相同类的方法 下一篇:模拟中的弹簧值注入

                  相关文章

                  最新文章

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

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

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