第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過 JUnit 模擬文件讀/寫

通過 JUnit 模擬文件讀/寫

白衣非少年 2022-05-12 18:21:14
你如何通過 JUnit 模擬文件讀/寫?這是我的場景MyHandler.javapublic abstract class MyHandler {    private String path = //..path/to/file/here    public synchronized void writeToFile(String infoText) {        // Some processing        // Writing to File Here        File file = FileUtils.getFile(filepath);        file.createNewFile();        // file can't be written, throw FileWriteException        if (file.canWrite()) {            FileUtils.writeByteArrayToFile(file, infoText.getBytes(Charsets.UTF_8));        } else {            throw new FileWriteException();        }    }    public String readFromFile() {        // Reading from File here        String infoText = "";        File file = new File(path);        // file can't be read, throw FileReadException        if (file.canRead()) {            infoText = FileUtils.readFileToString(file, Charsets.UTF_8);                } else {            throw FileReadException();        }        return infoText    }}MyHandlerTest.java@RunWith(PowerMockRunner.class)@PrepareForTest({    MyHandler.class})public class MyHandlerTest {    private static MyHandler handler = null;    // Some Initialization for JUnit (i.e @Before, @BeforeClass, @After, etc)    @Test(expected = FileWriteException.class)    public void writeFileTest() throws Exception {       handler.writeToFile("Test Write!");    }    @Test(expected = FileReadException.class)    public void readFileTest() throws Exception {       handler.readFromFile();    }}鑒于上述來源,文件不可寫(不允許寫權限)的場景是可以的,但是,當我嘗試做file不可讀的場景時(不允許讀權限)。它總是讀取文件,我已經(jīng)嘗試通過以下方式修改測試代碼的文件權限File f = new File("..path/to/file/here");f.setReadable(false);但是,我做了一些閱讀,setReadable()在 Windows 機器上運行時總是返回 false(失敗)。有沒有辦法以編程方式修改與 JUnit 相關的目標文件的文件權限?筆記無法修改要測試的目標源代碼,即不能修改 Myhandler.class的遺留代碼。
查看完整描述

3 回答

?
慕俠2389804

TA貢獻1719條經(jīng)驗 獲得超6個贊

不依賴于操作系統(tǒng)文件權限,而是使用 PowerMock 模擬 FileUtils.getFile(...) 并使其返回 File 的實例(例如匿名子類),該實例返回 canWrite()/canRead() 的特定值。



查看完整回答
反對 回復 2022-05-12
?
慕娘9325324

TA貢獻1783條經(jīng)驗 獲得超4個贊

由于 Mockito 不能模擬靜態(tài)方法,請改用File工廠(或?qū)⒛貥?gòu)FileUtils為工廠),然后您可以模擬它并返回一個模擬File實例,您還可以在其中模擬File您想要的任何方法。

因此,FileUtils.getFile(filepath)您現(xiàn)在將擁有類似的東西FileFactory.getInstance().getFile(filepath),例如,您可以getFile(String)輕松地模擬方法。


查看完整回答
反對 回復 2022-05-12
?
HUX布斯

TA貢獻1876條經(jīng)驗 獲得超6個贊

在 jUnit 中,對于像你這樣的場景有一個方便的規(guī)則。


public class MyHandlerTest {


    @Rule

    // creates a temp folder that will be removed after each test

    public org.junit.rules.TemporaryFolder folder = new org.junit.rules.TemporaryFolder();


    private MyHandler handler;


    @Before

    public void setUp() throws Exception {

        File file = folder.newFile("myFile.txt");

        // do whatever you need with it - fill with test content and so on.

        handler = new MyHandler(file.getAbsolutePath()); // use the real thing

    }


    // Test whatever behaviour you need with a real file and predefined dataset.

}


查看完整回答
反對 回復 2022-05-12
  • 3 回答
  • 0 關注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號