MockRestServiceServer:如何用主体模拟POST调用? [英] MockRestServiceServer: how to mock a POST call with a body?

查看:85
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆