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
提交
取消