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

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

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

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

      1. 测试方法之外的 Mockito 存根

        时间:2023-10-01
      2. <i id='UUaTI'><tr id='UUaTI'><dt id='UUaTI'><q id='UUaTI'><span id='UUaTI'><b id='UUaTI'><form id='UUaTI'><ins id='UUaTI'></ins><ul id='UUaTI'></ul><sub id='UUaTI'></sub></form><legend id='UUaTI'></legend><bdo id='UUaTI'><pre id='UUaTI'><center id='UUaTI'></center></pre></bdo></b><th id='UUaTI'></th></span></q></dt></tr></i><div id='UUaTI'><tfoot id='UUaTI'></tfoot><dl id='UUaTI'><fieldset id='UUaTI'></fieldset></dl></div>

          <legend id='UUaTI'><style id='UUaTI'><dir id='UUaTI'><q id='UUaTI'></q></dir></style></legend>
            <tbody id='UUaTI'></tbody>

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

              <tfoot id='UUaTI'></tfoot>

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

                • 本文介绍了测试方法之外的 Mockito 存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在测试方法之外有以下方法

                  I have the following method outside the test method

                  private DynamicBuild getSkippedBuild() {
                      DynamicBuild build = mock(DynamicBuild.class);
                      when(build.isSkipped()).thenReturn(true);
                      return build;
                  }
                  

                  但是当我调用这个方法时,我得到了以下错误

                  but when I call this method I get the following error

                  org.mockito.exceptions.misusing.UnfinishedStubbingException: 
                  Unfinished stubbing detected here:
                  -> at LINE BEING CALLED FROM
                  
                  E.g. thenReturn() may be missing.
                  Examples of correct stubbing:
                      when(mock.isOk()).thenReturn(true);
                      when(mock.isOk()).thenThrow(exception);
                      doThrow(exception).when(mock).someVoidMethod();
                  Hints:
                   1. missing thenReturn()
                   2. you are trying to stub a final method, you naughty developer!
                  

                  当您在测试方法之外存根时,mockito 似乎不高兴.不支持吗?

                  Looks like mockito is not happy when you stub outside the test method. Is that not supported ?

                  我可以通过在 @Test 方法中执行存根来实现此功能,但我想在 @Tests 中重复使用存根.

                  I can get this to work by doing the stubbing in @Test method but I want to reuse the stubbing across @Tests.

                  推荐答案

                  如果 isSkipped() 不是 final 方法,这个问题可能表明你尝试存根一种方法,而另一种方法的存根正在进行中.它不受支持,因为 Mockito 在其存根 API 中依赖于方法调用的顺序(when() 等).

                  If isSkipped() is not a final method, this problem probably indicates that you try to stub a method while stubbing of another method is in progress. It's not supported because Mockito relies on order of method invocations (when(), etc) in its stubbing API.

                  我猜你的测试方法中有这样的东西:

                  I guess you have something like this in your test method:

                  when(...).thenReturn(getSkippedBuild());
                  

                  如果是,则需要改写如下:

                  If so, you need to rewrite it as follows:

                  DynamicBuild build = getSkippedBuild();
                  when(...).thenReturn(build);
                  

                  这篇关于测试方法之外的 Mockito 存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Spring Boot 测试中的 MockBean 注解导致 NoUniqueBeanDe 下一篇:如何使用 Mockito 和 jUnit 模拟持久化和实体

                  相关文章

                  最新文章

                  <tfoot id='XzheH'></tfoot>

                      <bdo id='XzheH'></bdo><ul id='XzheH'></ul>
                  1. <legend id='XzheH'><style id='XzheH'><dir id='XzheH'><q id='XzheH'></q></dir></style></legend>

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

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