我需要为设计欠佳的旧应用程序编写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());