• <legend id='9gO9Z'><style id='9gO9Z'><dir id='9gO9Z'><q id='9gO9Z'></q></dir></style></legend>

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

        <small id='9gO9Z'></small><noframes id='9gO9Z'>

      1. 使用 Mockito 2 模拟最终课程

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

            <tbody id='YvICV'></tbody>

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

            <legend id='YvICV'><style id='YvICV'><dir id='YvICV'><q id='YvICV'></q></dir></style></legend>
            • <tfoot id='YvICV'></tfoot>

                1. 本文介绍了使用 Mockito 2 模拟最终课程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在从我目前正在处理的项目中删除 Powermock,因此我正在尝试仅使用 Mockito (mockito-core-2.2.28) 重写一些现有的单一测试.

                  I'm removing Powermock from the project I'm currently working on, so I'm trying to rewrite some existing unitary test only with Mockito (mockito-core-2.2.28).

                  当我运行测试时,出现以下错误:

                  When I run the test, I have the following error:

                  org.mockito.exceptions.base.MockitoException:

                  org.mockito.exceptions.base.MockitoException:

                  无法模拟/监视类 com.ExternalpackagePath.Externalclass

                  Cannot mock/spy class com.ExternalpackagePath.Externalclass

                  Mockito 无法模拟/窥探,因为:

                  Mockito cannot mock/spy because :

                  • 最后一节课

                  我知道这个问题已经被问过了(如何模拟带有 mockito 的 final 类,模拟对象调用 final使用 Mockito 类静态方法),但我没有找到我正在寻找的答案.

                  I know that this question has already been asked (How to mock a final class with mockito, Mock objects calling final classes static methods with Mockito), but I didn't find the answer I'm looking for.

                  这是我的代码摘录:

                  public class MyClassToTest extends TestCase {
                      private MyClass myClass;
                      @Mock private Externalclass ext;  // This class is final, I would like to mock it
                  
                      @Override
                      protected void setUp() throws Exception {
                          MockitoAnnotations.initMocks(this); // <<<< The exception is thrown here
                          ext = Mockito.mock(Externalclass.class);
                      }
                  }
                  

                  如 Mockito 文档中所述(https://github.com/mockito/mockito/wiki/What%27s-new-in-Mockito-2,§Mock the unmockable),我添加了 org.mockito.plugins.MockMaker 文件.这是我项目的树:

                  As mentioned in the Mockito documentation (https://github.com/mockito/mockito/wiki/What%27s-new-in-Mockito-2, §Mock the unmockable), I added the org.mockito.plugins.MockMaker file. This is the tree of my project :

                  • 项目
                    • 源代码
                      • com.packagePath.myPackage
                        • 我的班级
                        • com.packagePath.myPackage
                          • myClassToTest
                          • 模拟扩展
                            • org.mockito.plugins.MockMaker

                            我也尝试将资源"放在src"中的目录,在名为test"的子目录中,但结果仍然相同.

                            I also tries to put the "resources" directory in "src", in a subdir called "test", but the result is still the same.

                            我认为使用 Mockito v2 可以模拟决赛.有人知道这里缺少什么吗?

                            I thought that mocking a final was possible with Mockito v2. Does someone have an idea of what is missing here ?

                            谢谢!

                            推荐答案

                            好吧,我发现这里有什么问题,它可能对其他人有用.我的项目树错了,我把org.mockito.plugins.MockMaker直接放到了src"的一个目录mockito-extension"中.这是我现在的树:

                            Well, I found what's wrong here, it maybe useful for other people. My project tree is wrong, I put the org.mockito.plugins.MockMaker in a directory "mockito-extension" directly in "src". This is my tree now:

                            • 项目
                              • 源代码
                                • com.packagePath.myPackage
                                  • 我的班级
                                  • org.mockito.plugins.MockMaker
                                  • com.packagePath.myPackage
                                    • myClassToTest

                                    这篇关于使用 Mockito 2 模拟最终课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 mockito 模拟使用通配符返回泛型的方法 下一篇:如何用 mockito 模拟 builder

                  相关文章

                  最新文章

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

                    1. <small id='3x5xW'></small><noframes id='3x5xW'>

                      <tfoot id='3x5xW'></tfoot>
                        <bdo id='3x5xW'></bdo><ul id='3x5xW'></ul>