久久久久久久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(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 久久爱黑人激情av摘花 | 欧美高清一区 | 国产精品中文字幕在线 | 免费观看黄网站 | 国产精品久久久久久久久久不蜜臀 | 欧美在线一区二区三区 | 日韩视频一区 | 成年视频在线观看 | 亚洲视频在线观看一区二区三区 | 国户精品久久久久久久久久久不卡 | 欧美精品在线一区 | 久久久久国产 | 日本在线黄色 | 久久久久久高潮国产精品视 | 免费看黄色小视频 | 日韩一级免费电影 | 伦理午夜电影免费观看 | 自拍偷拍中文字幕 | 亚洲一区国产精品 | 在线观看欧美日韩视频 | 一区二区三区视频免费观看 | 99精品国产一区二区三区 | 日韩欧美在线观看 | 欧美综合一区 | 久草新在线 | 欧美一级久久 | 久久69精品久久久久久久电影好 | 六月色婷 | 午夜视频一区 | 亚洲精品乱码 | www.夜夜骑.com | 日韩国产一区二区三区 | 91精品无人区卡一卡二卡三 | 欧美日韩在线播放 | 亚洲bt 欧美bt 日本bt | 性做久久久久久免费观看欧美 | 精品欧美一区二区三区久久久 | 给我免费的视频在线观看 | 久久久久久久99 | 欧美一区二区三区小说 | 日韩在线免费电影 |