この質問は、この質問と密接に関連しています。違いは、クライアントをモックするという推奨されるアプローチに従うことです。したがって、次のHTTPBuilderを定義しています。
protected readUrl() {
def http = new HTTPBuilder("http://example.com")
def status = http.request(Method.GET, ContentType.JSON) {req ->
response.success = {resp, json ->
result = json.toString()
new Success<String>(result)
}
response.'401' = {resp ->
final String errMsg = "Not Authorized"
new Failed(Failable.Fail.ACCESS_DENIED, errMsg)
}
response.failure = {resp ->
final String errMsg = "General failure ${resp.statusLine}"
new Failed(Failable.Fail.UNKNOWN, errMsg)
}
}
}
私がやりたいのは、このコードブロックをユニットテストする方法を見つけることです。可能であれば、応答コードを具体的に設定できるように、応答をモックしたかったのです。誰かが私にこれを行う方法を教えてもらえますか?