我需要为一个设计很差的旧应用程序编写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);
}
Run Code Online (Sandbox Code Playgroud)
但是当它得到格式错误的XML或者不理解它返回的请求并将null一些东西写入标准输出时.
有没有办法在JUnit中断言控制台输出?要抓住像这样的案例:
System.out.println("match found: " + strExpr);
System.out.println("xml not well formed: " + e.getMessage());
Run Code Online (Sandbox Code Playgroud) 对stdIn / stdOut进行单元测试是常见的做法吗?如果是这样,那么您将如何测试以下内容:
import scala.io.StdIn._
object Test {
def main(args: Array[String]) = {
println("Please input your text. Leaving an empty line will indicate end of the input.")
val input = Iterator.continually(readLine()).takeWhile(_ != "").mkString("\n")
val result = doSomethingWithInput(input)
println("Result:")
println(result)
}
}
Run Code Online (Sandbox Code Playgroud)
我通常使用ScalaTest,如果有什么区别。