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)
添加回答
舉報
0/150
提交
取消