简体   繁体   English

最终 class 和最终方法 - Mocking 和 Mockito 2

[英]Final class and Final method - Mocking with Mockito 2

Is the feature provided by Mockito 2 to mock final methods and final classes using org.mockito.plugins.MockMaker(mock-maker-inline) generally available(released) or still in the incubation phase. Mockito 2 提供的功能是使用 org.mockito.plugins.MockMaker(mock-maker-inline) 来模拟最终方法和最终类,通常可用(已发布)或仍处于孵化阶段。 I am using mockito-core-2.23.4 artifact in my application.我在我的应用程序中使用了 mockito-core-2.23.4 工件。 Need suggestions in mocking the final classes and methods. mocking 最终类和方法中需要建议。 Is it advisable to use this approach or look for alternate options?是否建议使用这种方法或寻找替代选项?

As of Mockito 2.x, Mockito now supports mocking of final classes and methods.从 Mockito 2.x 开始,Mockito 现在支持最终类和方法的 mocking。

Example:例子:

Say we have a MyList class shown below as the collaborator in test cases.假设我们有一个如下所示的MyList class 作为测试用例中的协作者。

We'll add a new finalMethod to this class:我们将为这个 class 添加一个新的finalMethod

public class MyList extends AbstractList {
    final public int finalMethod() {
        return 0;
    }
}

And we'll also extend it with a final subclass:我们还将使用最终子类对其进行扩展:

public final class FinalList extends MyList {     
    @Override
    public int size() {
        return 1;
    }
}

Before Mockito can be used for mocking final classes and methods, it needs to be configured.在 Mockito 可以用于 mocking 最终类和方法之前,需要对其进行配置。

We need to add a text file to the project's src/test/resources/mockito-extensions directory named org.mockito.plugins.MockMaker and add a single line of text:我们需要在项目的 src/test/resources/mockito-extensions 目录中添加一个名为 org.mockito.plugins.MockMaker 的文本文件并添加一行文本:

mock-maker-inline模拟机内联

Mockito checks the extensions directory for configuration files when it is loaded. Mockito 在加载时检查配置文件的扩展目录。 This file enables the mocking of final methods and classes.此文件启用最终方法和类的 mocking。

Mock a Final Method:模拟最终方法:

Once Mockito is properly configured, a final method can be mocked like any other:正确配置 Mockito 后,可以像其他任何方法一样模拟最终方法:

@Test
public void whenMockFinalMethodMockWorks() {

    MyList myList = new MyList();

    MyList mock = mock(MyList.class);
    when(mock.finalMethod()).thenReturn(1);

    assertNotEquals(mock.finalMethod(), myList.finalMethod());
}

By creating a concrete instance and a mock instance of MyList, we can compare the values returned by both versions of finalMethod() and verify that the mock is called.通过创建 MyList 的具体实例和模拟实例,我们可以比较两个版本的finalMethod()返回的值并验证是否调用了模拟。

Mock a Final Class:模拟最终 Class:

Mocking a final class is just as easy as mocking any other class: Mocking 最终 class 与 mocking 任何其他 ZA2F2ED4F8EBC2CBB4C21A29DC40AB6 一样简单

@Test
public void whenMockFinalClassMockWorks() {

    FinalList finalList = new FinalList();

    FinalList mock = mock(FinalList.class);
    when(mock.size()).thenReturn(2);

    assertNotEquals(mock.size(), finalList.size());
}

Similar to the test above, we create a concrete instance and a mock instance of our final class, mock a method and verify that the mocked instance behaves differently.与上面的测试类似,我们创建最终 class 的具体实例和模拟实例,模拟一个方法并验证模拟实例的行为不同。

Reference: https://www.baeldung.com/mockito-final参考: https://www.baeldung.com/mockito-final

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM