亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

System.out.println()的JUnit測試

System.out.println()的JUnit測試

波斯汪 2019-06-20 16:31:56
System.out.println()的JUnit測試我需要為一個設計不佳的舊應用程序編寫JUnit測試,該應用程序正在為標準輸出編寫大量錯誤消息。當getResponse(String request)方法的正確行為-它返回一個XML響應:@BeforeClasspublic static void setUpClass() throws Exception {     Properties queries = loadPropertiesFile("requests.properties");     Properties responses = loadPropertiesFile("responses.properties");     instance = new ResponseGenerator(queries, responses);}@Testpublic void testGetResponse() {     String request = "<some>request</some>";     String expResult = "<some>response</some>";     String result = instance.getResponse(request);     assertEquals(expResult, result);}但是,當它得到格式錯誤的xml或不理解它返回的請求時null寫一些東西到標準輸出。是否有任何方法來斷言JUnit中的控制臺輸出?如:System.out.println("match found: " + strExpr);System.out.println("xml not well formed: " + e.getMessage());
查看完整描述

3 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

我知道這是一個舊線程,但是有一個很好的庫可以這樣做:

系統規則

文檔中的示例:

public void MyTest {
    @Rule
    public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();

    @Test
    public void overrideProperty() {
        System.out.print("hello world");
        assertEquals("hello world", systemOutRule.getLog());
    }}

它也會讓你困住System.exit(-1)以及其他需要測試的命令行工具。


查看完整回答
反對 回復 2019-06-20
?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

您可以通過以下方式設置System.out打印流SET()(以及為inerr)。您是否可以將其重定向到記錄到字符串的打印流,然后檢查它?這似乎是最簡單的機制。

(我主張,在某個階段,將應用程序轉換為某種日志框架-但我懷疑你已經意識到了這一點!)


查看完整回答
反對 回復 2019-06-20
  • 3 回答
  • 0 關注
  • 1345 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號