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

模擬系統類以獲取系統屬性

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

問題描述

我通過 Eclipse 中的 JVM 參數在系統變量中設置了一個文件夾路徑,我試圖在我的類中訪問它: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 測試方法時,我嘗試模擬此調用,因為測試類不考慮 JVM 參數.我使用 PowerMockito 模擬靜態 System 類,并嘗試在調用 System.getProperpty 時返回一些路徑.

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.

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

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),為測試用例而不是 System.class 準備我正在測試的類

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

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

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

這篇關于模擬系統類以獲取系統屬性的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 色综合久 | 中文字幕高清一区 | 精品久久一区 | 午夜精品久久久久久久久久久久久 | 日韩视频一级 | 国产精品久久久久久久久 | 亚洲精品乱码久久久久久9色 | 欧美一区免费 | 亚洲欧美成人影院 | 亚洲精品视频播放 | 一级高清免费毛片 | 中文字幕一区二区三区乱码在线 | 九九99九九精彩46 | 欧美一级小视频 | 国产精品成人av | 久青草影院 | 国产草草视频 | 北条麻妃一区二区三区在线观看 | 欧美成人精品一区二区三区 | 91视频久久| 日本中文在线视频 | 色综合天天天天做夜夜夜夜做 | 婷婷久久一区 | 毛片网站免费观看 | 亚洲在线观看视频 | 免费看黄色小视频 | 亚州综合一区 | 久久一区二区精品 | 午夜性视频 | 午夜国产 | 中文字幕免费视频 | 色本道| 欧美精品一区二区三区在线播放 | 亚洲韩国精品 | 91天堂| 亚洲国产成人精品女人久久久野战 | 九九热精品视频 | 九九热免费视频在线观看 | 国产一区二区欧美 | 天天干视频 | 亚洲综合大片69999 |