uzi*_*lan 8 rest unit-testing scala
我想使用Spec2和Scala来测试使用Java构建的REST服务.我看了一下Spray但它接缝就像你必须使用Spray构建你的应用程序才能使用SprayTest测试它.我也找到了这个帖子,但它并不是我想要的.
还有其他想法吗?
我们已使用 Specs2 和 Dispatch 库 ( https://dispatchhttp.org/Dispatch.html ) 成功测试了所有 REST API。Dispatch 需要一点时间来理解,但是一旦您了解了它如何与各种运算符一起组合所有内容,您就可以使用几行代码来测试简单的 REST 服务。
以下是最近项目中的几个测试用例:
def issueErrorStatus = {
val requestBody = "msisdn=447777666666&message=Some test message"
val req = url("http://localhost:%d/otac/issue".format(port)) <<
(requestBody, "application/x-www-form-urlencoded")
val response = Http.when(_ == 400)(req <:< (touchPointHeaders) as_str)
response must_== """{"error":"Message must contain an {OTAC} place holder"}"""
}
def checkOtac = {
val req = url("http://localhost:%d/otac/check".format(port)) <<?
Vector(("msisdn" -> "447777123456"))
val response = Http(req <:< (touchPointHeaders) as_str)
response must_== """{"status":"Present","reissueAllowed":true}"""
}
Run Code Online (Sandbox Code Playgroud)
第一个测试发出 post 请求,第二个测试发出 get 请求。我们还有一些更复杂的测试,通过 lift-json 解析器解析响应 JSON 字符串,以便我们可以更轻松地再次断言文档。上述测试只是检查一些简单的错误/状态情况。
还有一个正在进行中的调度重启项目,该项目具有简化的 API 并可使用异步连接。但不确定它有多稳定。
归档时间: |
|
查看次数: |
3937 次 |
最近记录: |