MockRestServiceServer:如何用主体模拟POST调用? [英] MockRestServiceServer: how to mock a POST call with a body?
本文介绍了MockRestServiceServer:如何用主体模拟POST调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过以下方式用MockRestServiceServer
模拟POST方法:
I am trying to mock a POST method with MockRestServiceServer
in the following way:
MockRestServiceServer server = bindTo(restTemplate).build();
server.expect(requestTo("/my-api"))
.andExpect(method(POST))
.andRespond(withSuccess(expectedResponce, APPLICATION_JSON));
问题:如何在此设置中验证请求正文?
Problem: How do I verify a request body in this setup?
I browsed through the documentation and some examples and still can't figure out how it can be done.
推荐答案
You can use content().string to verify body:
.andExpect(content().string(expectedContent))
.andExpect(content().string(expectedContent))
或 content().bytes :
this.mockServer.expect(content().bytes("foo" .getBytes()))
this.mockServer.expect(content().bytes("foo".getBytes()))
this.mockServer.expect(content().string("foo"))
this.mockServer.expect(content().string("foo"))
这篇关于MockRestServiceServer:如何用主体模拟POST调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文