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

Mockito Spy - 調用構造函數之前的存根

Mockito Spy - stub before calling the constructor(Mockito Spy - 調用構造函數之前的存根)
本文介紹了Mockito Spy - 調用構造函數之前的存根的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試監視一個對象,并且我想在構造函數調用它之前存根由構造函數調用的方法.
我的班級是這樣的:

I'm trying to spy on an Object and I want to stub a method that is called by the constructor before the constructor calls it.
My class looks like that:

public class MyClass {
    public MyClass() {
         setup();
    }

    public void setup() {

    }
}

不得調用 setup 方法.那么,我如何監視這個方法(以及存根設置,使其什么都不做)?
它可以很好地模擬該方法,但我想對 MyClass 進行單元測試,所以我需要其他方法.

The setup method mustn't be called. Well, how do I spy on this method (and stub setup so that it does nothing)?
It works fine with mocking the method but I want to unit test MyClass and so I will need very other method.

為什么需要存根設置方法以使其不執行任何操作:
我正在編寫樂高機器人(lejos),并在設置中放置了一些機器人需要工作的代碼.但是,當我在 TinyVM(安裝在機器人上的 VM)之外調用它時,java 崩潰,因為它沒有正確初始化 VM(因為測試在我的 PC 上運行).對于單元測試,設置并不重要.
我不能存根類/方法設置調用,因為其中一些是公共靜態最終變量.

The reason why need to stub the setup method so that it does nothing:
I'm programing a Lego robot (lejos) and I put some code in setup that the robot needs to work. However, when I call it outside TinyVM (the VM that is installed on the robot), java crashes since it the VM hasn't been initialized properly (because the tests run on my PC). For unit-testing the setup isn't important.
I can't stub the classes/methods setup calls since some of them are public static final variables.

推薦答案

感謝您的建議,但它有點太復雜了.
我最終通過擴展類并覆蓋我的設置方法來模擬該方法.這樣默認構造函數就不會調用它的 setup 實現,而是調用被覆蓋的方法.
代碼如下:

Thanks for the suggestions, but it was a little bit too complex.
I ended up mocking the method by extending the class and overwriting my setup method. This way the default constructor won't call its implementation of setup, it will call the overwritten method instead.
Here is the code:

// src/author/MyClass.java

public class MyClass {
    public MyClass() {
        setup();
    }

    protected void setup() {
        throw new Exception("I hate unit testing !");
    }

    public boolean doesItWork() {
        return true;
    }
}

// test/author/MyClass.java

public class MyClassTest {
    private class MockedMyClass extends MyClass {
        @Override
        protected void setup() {

        }
    }

    private MyClass instance;

    @Before
    public void setUp() { // Not to be confusing with `MyClass#setup()`!
        instance = new MockedMyClass();
    }

    @Test
    public void test_doesItWork() {
        assertTrue(instance.doesItWork());
    }

}

如果您不希望 MyTest 的 setup 方法被除您的測試之外的其他子類調用或覆蓋(因為其他開發人員可能會使用 setup 方法將事情搞砸),只需將可見性更改為默認值,并且只更改您的類將能夠調用設置.

If you don't want MyTest's setup method to do called or overwritten by other subclasses except your test (because other developer might mess things up very badly by using the setup method), just change the visibility to default and only your classes will be able to call setup.

如果有更簡單的方法,請回答問題,因為我對我的解決方案不是 100% 滿意.

If there is a simpler way, please answer the question because I'm not 100% content with my solution.

這篇關于Mockito Spy - 調用構造函數之前的存根的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 亚洲综合色站 | 成人欧美一区二区三区黑人孕妇 | 四虎免费视频 | 超碰8 | 一道本视频 | 96国产精品久久久久aⅴ四区 | 在线一区观看 | 精品视频一区二区三区在线观看 | 亚洲视频一区二区三区 | 精品91视频| 日韩精品 电影一区 亚洲 | 中文字幕日韩欧美一区二区三区 | 国产精品久久久久久吹潮 | 草逼网站| 亚洲精品久久久9婷婷中文字幕 | 日韩视频免费看 | 特一级黄色毛片 | 亚洲激情一级片 | 日韩一区二区在线视频 | 国产一区二区毛片 | 国产成人精品一区二区 | 性欧美xxxx | 蜜桃视频在线观看免费视频网站www | 在线观看亚洲专区 | 性色av一区 | 国产精品美女一区二区 | 免费v片在线观看 | 亚洲午夜精品久久久久久app | 国产成人av在线播放 | 自拍视频网站 | 日韩第一夜 | 欧美日韩精品专区 | 日本精品视频在线 | 亚洲喷水 | 日韩成人在线观看 | 欧美精品一区二区三区四区五区 | 久久成人免费视频 | 国产一区二区三区视频免费观看 | 国产精品自拍啪啪 | 免费午夜电影 | 国产精品自产av一区二区三区 |