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

      <tfoot id='jOUVI'></tfoot>

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

      1. 例外:mockito 想要但没有被调用,实际上与这个

        时间:2023-09-26
        <tfoot id='GlXgQ'></tfoot>

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

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

                <tbody id='GlXgQ'></tbody>
                • <bdo id='GlXgQ'></bdo><ul id='GlXgQ'></ul>
                  <legend id='GlXgQ'><style id='GlXgQ'><dir id='GlXgQ'><q id='GlXgQ'></q></dir></style></legend>
                • 本文介绍了例外:mockito 想要但没有被调用,实际上与这个 mock 的交互为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有界面

                  Interface MyInterface {
                    myMethodToBeVerified (String, String);
                  }
                  

                  接口的实现是

                  class MyClassToBeTested implements MyInterface {
                     myMethodToBeVerified(String, String) {
                      …….
                     }
                  }
                  

                  我还有一门课

                  class MyClass {
                      MyInterface myObj = new MyClassToBeTested();
                      public void abc(){
                           myObj.myMethodToBeVerified (new String("a"), new String("b"));
                      }
                  }
                  

                  我正在尝试为 MyClass 编写 JUnit.我已经完成了

                  I am trying to write JUnit for MyClass. I have done

                  class MyClassTest {
                      MyClass myClass = new MyClass();
                    
                      @Mock
                      MyInterface myInterface;
                  
                      testAbc(){
                           myClass.abc();
                           verify(myInterface).myMethodToBeVerified(new String("a"), new String("b"));
                      }
                  }
                  

                  但是我得到 mockito 想要但没有被调用,实际上在验证调用时与这个 mock 的交互为零.

                  谁能提出一些解决方案.

                  can anyone suggest some solutions.

                  推荐答案

                  你需要在你正在测试的类中注入 mock.目前,您正在与真实对象进行交互,而不是与模拟对象进行交互.您可以通过以下方式修复代码:

                  You need to inject mock inside the class you're testing. At the moment you're interacting with the real object, not with the mock one. You can fix the code in a following way:

                  void testAbc(){
                       myClass.myObj = myInteface;
                       myClass.abc();
                       verify(myInterface).myMethodToBeVerified(new String("a"), new String("b"));
                  }
                  

                  虽然将所有初始化代码提取到 @Before

                  although it would be a wiser choice to extract all initialization code into @Before

                  @Before
                  void setUp(){
                       myClass = new myClass();
                       myClass.myObj = myInteface;
                  }
                  
                  @Test
                  void testAbc(){
                       myClass.abc();
                       verify(myInterface).myMethodToBeVerified(new String("a"), new String("b"));
                  }
                  

                  这篇关于例外:mockito 想要但没有被调用,实际上与这个 mock 的交互为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使用相同的参数验证对同一模拟方法的调用 下一篇:是否可以在 PowerMock 中对私有静态方法使用部分模

                  相关文章

                  最新文章

                  <tfoot id='vNd3j'></tfoot>

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

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

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