久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

用 mockito 模擬單例

Mocking a singleton with mockito(用 mockito 模擬單例)
本文介紹了用 mockito 模擬單例的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我需要測試一些在方法調用中使用單例的遺留代碼.測試的目的是確保類 sunder 測試調用單例方法.我在 SO 上看到過類似的問題,但所有答案都需要其他依賴項(不同的測試框架)——不幸的是,我僅限于使用 Mockito 和 JUnit,但使用這種流行的框架應該是完全可能的.

I need to test some legacy code, which uses a singleton in a a method call. The purpose of the test is to ensure that the clas sunder test makes a call to singletons method. I have seen similar questions on SO, but all the answers require other dependencies (different test frameworks) - I'm unfortunately limited to using Mockito and JUnit, but this should be perfectly possible with such popular framework.

單身人士:

public class FormatterService {

    private static FormatterService INSTANCE;

    private FormatterService() {
    }

    public static FormatterService getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new FormatterService();
        }
        return INSTANCE;
    }

    public String formatTachoIcon() {
        return "URL";
    }

}

被測類:

public class DriverSnapshotHandler {

    public String getImageURL() {
        return FormatterService.getInstance().formatTachoIcon();
    }

}

單元測試:

public class TestDriverSnapshotHandler {

    private FormatterService formatter;

    @Before
    public void setUp() {

        formatter = mock(FormatterService.class);

        when(FormatterService.getInstance()).thenReturn(formatter);

        when(formatter.formatTachoIcon()).thenReturn("MockedURL");

    }

    @Test
    public void testFormatterServiceIsCalled() {

        DriverSnapshotHandler handler = new DriverSnapshotHandler();
        handler.getImageURL();

        verify(formatter, atLeastOnce()).formatTachoIcon();

    }

}

這個想法是配置可怕的單例的預期行為,因為被測試的類將調用它的 getInstance 和 formatTachoIcon 方法.不幸的是,這失敗并顯示錯誤消息:

The idea was to configure the expected behaviour of the dreaded singleton, since the class under test will call it's getInstance and then formatTachoIcon methods. Unfortunately this fails with an error message:

when() requires an argument which has to be 'a method call on a mock'.

推薦答案

你的問題是不可能的,因為你的遺留代碼依賴于一個靜態方法 getInstance() 而 Mockito 不允許模擬靜態方法,所以下面這行不起作用

What you are asking is not possible because your legacy code relies on a static method getInstance() and Mockito does not allow to mock static methods, so the following line won't work

when(FormatterService.getInstance()).thenReturn(formatter);

解決這個問題有兩種方法:

There are 2 ways around this problem:

  1. 使用允許模擬靜態方法的不同模擬工具,例如 PowerMock.

  1. Use a different mocking tool, such as PowerMock, that allows to mock static methods.

重構你的代碼,讓你不依賴靜態方法.我能想到的實現這一點的侵入性最小的方法是向 DriverSnapshotHandler 添加一個構造函數,該構造函數注入一個 FormatterService 依賴項.此構造函數將僅在測試中使用,您的生產代碼將繼續使用真正的單例實例.

Refactor your code, so that you don't rely on the static method. The least invasive way I can think of to achieve this is by adding a constructor to DriverSnapshotHandler that injects a FormatterService dependency. This constructor will be only used in tests and you production code will continue to use the real singleton instance.

public static class DriverSnapshotHandler {

    private final FormatterService formatter;

    //used in production code
    public DriverSnapshotHandler() {
        this(FormatterService.getInstance());
    }

    //used for tests
    DriverSnapshotHandler(FormatterService formatter) {
        this.formatter = formatter;
    }

    public String getImageURL() {
        return formatter.formatTachoIcon();
    }
}

那么,你的測試應該是這樣的:

Then, your test should look like this :

FormatterService formatter = mock(FormatterService.class);
when(formatter.formatTachoIcon()).thenReturn("MockedURL");
DriverSnapshotHandler handler = new DriverSnapshotHandler(formatter);
handler.getImageURL();
verify(formatter, atLeastOnce()).formatTachoIcon();

這篇關于用 mockito 模擬單例的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to mock super reference (on super class)?(如何模擬超級參考(在超級類上)?)
Java mock database connection(Java 模擬數據庫連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無法投射模擬)
Set value to mocked object but get null(將值設置為模擬對象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 天天草草草 | 成人欧美| 欧美三级在线 | 99精品久久久 | www.成人.com| 99福利在线观看 | 国产一区 在线视频 | 欧美激情精品久久久久久 | 欧美另类视频 | 欧美在线一区二区视频 | 精品自拍视频在线观看 | 龙珠z国语版在线观看 | 国产视频91在线 | 91高清在线视频 | 性xxxxx| av日韩在线播放 | av官网在线 | 久草电影网| 欧美久久天堂 | 国产欧美一区二区三区在线看蜜臀 | 羞羞的视频免费观看 | 国产一区二区三区在线 | 国产高清视频 | 国产成人精品一区二 | 在线看亚洲 | 嫩草研究影院 | 亚洲最色网站 | 欧洲妇女成人淫片aaa视频 | 欧美极品少妇xxxxⅹ免费视频 | 麻豆av电影网 | 久久精品中文 | 一区二区免费高清视频 | 91视视频在线观看入口直接观看 | 午夜精品一区二区三区三上悠亚 | 成人毛片视频在线播放 | 成人精品视频在线观看 | 密乳av | 国产成人一区二区三区 | 久久精品一级 | 日日操av| 69av在线视频 |