用 Mockito 2 模拟最后一堂课

编程入门 行业动态 更新时间:2024-10-27 06:18:30
本文介绍了用 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 :

  • 最后一节课

我知道这个问题已经有人问过了(How to mock带有 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 文档中所述(github/mockito/mockito/wiki/What%27s-new-in-Mockito-2, §Mock the unmockable),我添加了 org.mockito.plugins.MockMaker 文件.这是我项目的树:

As mentioned in the Mockito documentation (github/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
          • mockito 扩展
            • 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 模拟最后一堂课

本文发布于:2023-10-31 07:09:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545387.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:堂课   Mockito

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!