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

    • <bdo id='3tb88'></bdo><ul id='3tb88'></ul>
    <legend id='3tb88'><style id='3tb88'><dir id='3tb88'><q id='3tb88'></q></dir></style></legend>

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

      使用 Mockito 在另一个类中模拟一个类方法

      Using Mockito to mock a class method inside another class(使用 Mockito 在另一个类中模拟一个类方法)

        <tbody id='M9ypr'></tbody>
      <tfoot id='M9ypr'></tfoot>

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

            • <legend id='M9ypr'><style id='M9ypr'><dir id='M9ypr'><q id='M9ypr'></q></dir></style></legend>
            • <small id='M9ypr'></small><noframes id='M9ypr'>

            • <i id='M9ypr'><tr id='M9ypr'><dt id='M9ypr'><q id='M9ypr'><span id='M9ypr'><b id='M9ypr'><form id='M9ypr'><ins id='M9ypr'></ins><ul id='M9ypr'></ul><sub id='M9ypr'></sub></form><legend id='M9ypr'></legend><bdo id='M9ypr'><pre id='M9ypr'><center id='M9ypr'></center></pre></bdo></b><th id='M9ypr'></th></span></q></dt></tr></i><div id='M9ypr'><tfoot id='M9ypr'></tfoot><dl id='M9ypr'><fieldset id='M9ypr'></fieldset></dl></div>
                本文介绍了使用 Mockito 在另一个类中模拟一个类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试使用 Mockito/JUnit 为这样的函数编写单元测试:

                I'm trying to write unit tests with Mockito / JUnit for a function like this:

                class1 {
                 method {
                  object1 = class2.method // method that I want to fake the return value
                  // some code that I still want to run
                 }
                }
                

                在 Mockito 中有什么方法可以存根 class2.method 的结果吗?我正在尝试提高 class1 的代码覆盖率,因此我需要调用它的实际生产方法.

                Is there any way in Mockito to stub the result of class2.method? I'm trying to improve code coverage for class1 so I need to call its real production methods.

                我查看了 Mockito API 的 spy 方法,但这会覆盖整个方法,而不是我想要的部分.

                I looked into the Mockito API at its spy method but that would overwrite the whole method and not the part that I want.

                推荐答案

                我想我理解你的问题.让我重新表述一下,您有一个正在尝试测试的函数,并且想要模拟在该函数中调用的函数的结果,但在不同的类中.我已经通过以下方式处理了.

                I think I am understanding your question. Let me re-phrase, you have a function that you are trying to test and want to mock the results of a function called within that function, but in a different class. I have handled that in the following way.

                public MyUnitTest {
                    private static final MyClass2 class2 = mock(MyClass2.class);
                
                    @Begin
                    public void setupTests() {
                        when(class2.get(1000)).thenReturn(new User(1000, "John"));
                        when(class2.validateObject(anyObj()).thenReturn(true);
                    }
                
                    @Test
                    public void testFunctionCall() {
                        String out = myClass.functionCall();
                        assertThat(out).isEqualTo("Output");
                    }
                }
                

                这样做是在使用 @Before 注释包装的函数中,我正在设置我希望 class2 中的函数如何响应给定的特定输入.然后,在实际测试中,我只是在我想要测试的类中调用我试图测试的函数.在这种情况下,myClass.functionCall() 正常运行,您不会覆盖它的任何方法,而只是模拟它从 MyClass2 中的方法(或方法)获得的输出.

                What this is doing is that within the function wrapped with the @Before annotation, I am setting up how I want the functions in class2 to respond given specific inputs. Then, from within the actual test, I am just calling the function that I am trying to test in the class I want to test. In this case, the myClass.functionCall() is running through as normal and you are not overwriting any of its methods, but you are just mocking the outputs that it gets from the methods (or method) within MyClass2.

                这篇关于使用 Mockito 在另一个类中模拟一个类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
                Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
                How to convert Integer to int?(如何将整数转换为整数?)
                How do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
                Inconsistent behavior on java#39;s ==(java的行为不一致==)
                Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)

                    <bdo id='zqnE3'></bdo><ul id='zqnE3'></ul>
                    • <tfoot id='zqnE3'></tfoot><legend id='zqnE3'><style id='zqnE3'><dir id='zqnE3'><q id='zqnE3'></q></dir></style></legend>

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