最终类和最终方法 – 使用Mockito 2进行模拟

huangapple go评论96阅读模式
英文:

Final class and Final method - Mocking with Mockito 2

问题

由Mockito 2提供的用于模拟final方法和final类的功能,使用org.mockito.plugins.MockMaker(mock-maker-inline),是通用可用(已发布)还是仍处于孵化阶段。我正在我的应用程序中使用mockito-core-2.23.4工件。在模拟final类和方法时需要建议。是否建议使用这种方法或寻找替代选项?

英文:

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. I am using mockito-core-2.23.4 artifact in my application. Need suggestions in mocking the final classes and methods. Is it advisable to use this approach or look for alternate options?

答案1

得分: 2

从 Mockito 2.x 开始,Mockito 现在支持对最终类和方法进行模拟。

示例:

假设我们有一个在测试用例中作为协作者的 MyList 类,如下所示。

我们将在这个类中添加一个新的 finalMethod

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

然后我们将通过一个最终子类来扩展它:

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

在可以使用 Mockito 进行模拟最终类和方法之前,需要对其进行配置。

我们需要在项目的 src/test/resources/mockito-extensions 目录下添加一个名为 org.mockito.plugins.MockMaker 的文本文件,并添加一行文本:

mock-maker-inline

Mockito 在加载时会检查扩展目录中的配置文件。这个文件使得可以模拟最终方法和类。

模拟最终方法:

一旦 Mockito 正确配置,最终方法可以像其他方法一样进行模拟:

@Test
public void whenMockFinalMethodMockWorks() {
 
    MyList myList = new MyList();
 
    MyList mock = mock(MyList.class);
    when(mock.finalMethod()).thenReturn(1);
 
    assertNotEquals(mock.finalMethod(), myList.finalMethod());
}

通过创建一个 MyList 的实例和一个 mock 实例,我们可以比较两个版本的 finalMethod() 返回的值,并验证模拟是否被调用。

模拟最终类:

模拟最终类与模拟其他类一样简单:

@Test
public void whenMockFinalClassMockWorks() {
 
    FinalList finalList = new FinalList();
 
    FinalList mock = mock(FinalList.class);
    when(mock.size()).thenReturn(2);
 
    assertNotEquals(mock.size(), finalList.size());
}

与上面的测试类似,我们创建了最终类的一个具体实例和一个模拟实例,模拟一个方法并验证模拟实例的行为是否不同。

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

英文:

As of Mockito 2.x, Mockito now supports mocking of final classes and methods.

Example:

Say we have a MyList class shown below as the collaborator in test cases.

We’ll add a new finalMethod to this class:

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.

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:

> mock-maker-inline

Mockito checks the extensions directory for configuration files when it is loaded. This file enables the mocking of final methods and classes.

Mock a Final Method:

Once Mockito is properly configured, a final method can be mocked like any other:

@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.

Mock a Final Class:

Mocking a final class is just as easy as mocking any other class:

@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.

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

huangapple
  • 本文由 发表于 2020年4月8日 06:30:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/61090441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定