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

模擬系統(tǒng)類以獲取系統(tǒng)屬性

Mock System class to get system properties(模擬系統(tǒng)類以獲取系統(tǒng)屬性)
本文介紹了模擬系統(tǒng)類以獲取系統(tǒng)屬性的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我通過 Eclipse 中的 JVM 參數(shù)在系統(tǒng)變量中設(shè)置了一個(gè)文件夾路徑,我試圖在我的類中訪問它:System.getProperty("my_files_path").

I have a folder path set in system variable through JVM arguments in Eclipse and I am trying to access it in my class as: System.getProperty("my_files_path").

在為此類編寫 junit 測(cè)試方法時(shí),我嘗試模擬此調(diào)用,因?yàn)闇y(cè)試類不考慮 JVM 參數(shù).我使用 PowerMockito 模擬靜態(tài) System 類,并嘗試在調(diào)用 System.getProperpty 時(shí)返回一些路徑.

While writing junit test method for this class, I tried mocking this call as test classes do not consider JVM arguments. I have used PowerMockito to mock static System class and tried returning some path when System.getProperpty is being called.

在類級(jí)別有 @RunWith(PowerMockRunner.class)@PrepareForTest(System.class) 注釋.但是, System 類沒有被嘲笑,因此我總是得到空結(jié)果.任何幫助表示贊賞.

Had @RunWith(PowerMockRunner.class) and @PrepareForTest(System.class) annotations at class level. However, System class is not getting mocked as a result I always get null result. Any help is appreciated.

推薦答案

感謝 Satish.除了稍作修改外,此方法有效.我寫了 PrepareForTest(PathFinder.class),為測(cè)試用例而不是 System.class 準(zhǔn)備我正在測(cè)試的類

Thanks Satish. This works except with a small modification. I wrote PrepareForTest(PathFinder.class), preparing the class I am testing for test cases instead of System.class

另外,由于模擬只工作一次,我在模擬后立即調(diào)用了我的方法.我的代碼僅供參考:

Also, as mock works only once, I called my method right after mocking. My code just for reference:

@RunWith(PowerMockRunner.class)
@PrepareForTest(PathInformation.class)
public class PathInformationTest {

    private PathFinder pathFinder = new PathFinder();

@Test
    public void testValidHTMLFilePath() { 
        PowerMockito.mockStatic(System.class);
        PowerMockito.when(System.getProperty("my_files_path")).thenReturn("abc");
        assertEquals("abc",pathFinder.getHtmlFolderPath());
    }
}

這篇關(guān)于模擬系統(tǒng)類以獲取系統(tǒng)屬性的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to mock super reference (on super class)?(如何模擬超級(jí)參考(在超級(jí)類上)?)
Java mock database connection(Java 模擬數(shù)據(jù)庫連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無法投射模擬)
Set value to mocked object but get null(將值設(shè)置為模擬對(duì)象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調(diào)用驗(yàn)證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 中文字幕在线观看日本 | 欧美日韩一区二区三区视频 | 成人久久视频 | 日韩在线视频一区 | 一级黄色av| 亚洲综合在线视频 | 国产黄视频在线观看 | 福利视频网址导航 | 国产一及片 | 国产永久在线 | 午夜av片| 国产精品久久久久久久成人午夜 | 国产午夜免费视频 | 天天操夜夜操狠狠操 | 欧美性猛交xxxx黑人猛交 | 欧美一区二区三区在线播放 | 国产一级视频在线观看 | 日本少妇中文字幕 | 成人动漫免费观看 | 国产视频一区在线 | 超碰免费在线 | 嫩草嫩草嫩草嫩草 | 91福利区 | 亚洲成人精品在线 | 中文字幕婷婷 | 在线观看av的网站 | 国产成年人视频 | 一级大片免费看 | 国产日韩在线播放 | 免费在线小视频 | 亚洲免费在线播放 | 国产激情视频在线观看 | 在线一区二区三区 | 天天看天天爽 | 久久青草视频 | 一区二区三区影院 | 欧美视频二区 | 亚洲成肉网| 黄色成人小视频 | 四虎影视大全 | 亚洲精品成a人在线观看 |