Mock Final Classes and Methods with Mockito – 用Mockito模拟最终类和方法

最后修改: 2017年 11月 26日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this short tutorial, we’ll focus on how to mock final classes and methods using Mockito.

在这个简短的教程中,我们将重点介绍如何使用Mockito来模拟最终的类和方法。

As with other articles focused on the Mockito framework (such as Mockito Verify, Mockito When/Then and Mockito’s Mock Methods), we’ll use the MyList class shown below as the collaborator in test cases.

与其他关注Mockito框架的文章一样(如Mockito验证Mockito When/ThenMockito的模拟方法),我们将使用下面所示的MyList类作为测试案例的协作者。

We’ll add a new method for this tutorial:

我们将为本教程添加一个新方法。

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

And we’ll also extend it with a final subclass:

而且我们还将用一个final子类来扩展它。

public final class FinalList extends MyList {

    @Override
    public int size() {
        return 1;
    }
}

2. Configure Mockito for Final Methods and Classes

2.为最终方法和类配置Mockito

Before we can use Mockito for mocking final classes and methods, we have to configure it.

在我们使用Mockito来模拟最终的类和方法之前,我们必须对它进行配置。

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. This file enables the mocking of final methods and classes.

Mockito在加载时检查extensions目录中的配置文件。这个文件可以实现对最终方法和类的嘲弄。

3. Mock a Final Method

3.模拟最终方法

Once we’ve properly configured Mockito, we can mock a final method like any other:

一旦我们正确地配置了Mockito,我们就可以像其他方法一样模拟一个最终方法

@Test
public void whenMockFinalMethodMockWorks() {

    MyList myList = new MyList();

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

    assertThat(mock.finalMethod()).isNotEqualTo(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的具体实例和mock实例,我们可以比较finalMethod()的两个版本的返回值,并验证mock是否被调用。

4. Mock a Final Class

4.模拟期末考试

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);

    assertThat(mock.size()).isNotEqualTo(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.

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

5. Conclusion

5.结论

In this quick article, we covered how to mock final classes and methods with Mockito by using a Mockito extension.

在这篇快速文章中,我们介绍了如何通过使用Mockito扩展来用Mockito模拟最终的类和方法。

The full examples, as always, can be found over on GitHub.

完整的示例一如既往地可以在GitHub上找到