JUnit test for System.out.println()
我需要为一个设计不佳的旧应用程序编写JUnit测试,并且正在向标准输出编写大量错误消息。当该方法行为正确时,它将返回 XML 响应:getResponse(String request)
@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());