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

    2. <small id='WgTPi'></small><noframes id='WgTPi'>

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

      测试是否调用了另一个方法

      时间:2023-09-25

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

            <tbody id='tGfQx'></tbody>
        1. <legend id='tGfQx'><style id='tGfQx'><dir id='tGfQx'><q id='tGfQx'></q></dir></style></legend>

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

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

                问题描述

                所以我确定那里有类似的东西,但我已经搜索了一个小时,但还没有找到我正在寻找的东西.假设我有一个看起来像这样的类:

                So I'm sure there is something like this out there but I have been searching for an hour and haven't found exactly what I am looking for. say I have a class that looks like this:

                public class MyClass
                {
                    public void myMethod(boolean shouldCallOtherMethod)
                    {
                        if(shouldCallOtherMethod)
                        {
                            otherMethod();
                        }
                    }
                
                    public void otherMethod()
                    {
                        System.out.println("Called");
                    }
                }
                

                我该如何做这样的事情?

                How do I make something like this work?

                @Test
                public void shouldCallMethod()
                {
                    MyClass myClass = new MyClass();
                    myClass.myMethod(true)
                
                    // verify myClass.otherMethod method was called
                }
                

                推荐答案

                使用 Mockito,你可以做到 像这样监视真实物体:

                Using Mockito, you can do spying on real objects like this:

                import org.junit.Test;
                import static org.mockito.Mockito.*;
                public class MyClassTest {
                    @Test
                    public void otherMethodShouldBeCalled() {
                        MyClass myClass = new MyClass();
                        MyClass spy = spy(myClass);
                
                        spy.myMethod(true);
                        verify(spy).otherMethod();
                    }
                }
                

                有一些问题,请查看相关文档 也是如此.

                There are some gotchas, so take a look at the relevant documentation as well.

                这篇关于测试是否调用了另一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何使用 Mockito 或任何其他相关的 Java 框架来模 下一篇:使用原始参数模拟方法时 Mockito 中的 NullPointerE

                相关文章

                最新文章

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

                  <tfoot id='W6Z4z'></tfoot>

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

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