HTTPBuilder をモックするテストを書いています。HTTBuilder を使用するメソッド呼び出しは次のとおりです。
def http = new HTTPBuilder('http://localhost:8010')
public registerUpdate(Long id, Long version){
try{
http.request(Method.POST, JSON){ req ->
body = [
version: version,
id: id
]
response.success = {resp, json ->
log.warn "cached object id: $id and version: $version; status code: " + resp.statusLine.statusCode
}
}
}catch(Exception e){
log.warn('Failure Initiate Connection with Node Driver: ' + e.message);
}
テストでは、適切なメソッド、contentType、および body パラメーターが適切に設定されていることを確認します。
def "some test"(){
setup:
def httpBuildMock = new MockFor(HTTPBuilder.class)
httpBuildMock.demand.request{
met, type, body ->
assert met == Method.POST
assert type == ContentType.JSON
assert 10 == body.version
// def resp = body.call(null)
}
def mockService = httpBuildMock.proxyInstance()
service.http = mockService
when:
service.registerUpdate(1,2)
then:
httpBuildMock.verify mockService
このテストでは、body.version のアサーションは機能しません。それは「missingPropertyException」を与えます。デバッグ モードでは、body パラメーターにプロパティ 'id' と 'version' がありますが、それでも例外が発生することがわかります。「body」パラメーターをアサートするにはどうすればよいですか? ありがとうございました