【发布时间】:2010-11-10 07:14:31
【问题描述】:
我需要为一个设计不佳并且正在向标准输出写入大量错误消息的旧应用程序编写 JUnit 测试。当getResponse(String request) 方法正常运行时,它会返回一个 XML 响应:
@BeforeClass
public static void setUpClass() throws Exception {
Properties queries = loadPropertiesFile("requests.properties");
Properties responses = loadPropertiesFile("responses.properties");
instance = new ResponseGenerator(queries, responses);
}
@Test
public 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());
【问题讨论】: