如何使用MOCKMVC传递普通(未注释的API参数)对象

发布于 2025-01-22 12:55:54 字数 1274 浏览 0 评论 0 原文

我试图通过MockMVC库将常规对象传递给API。 这是一个示例(partiucalary filterProperties属性对象):

api:

@GetMapping("/test-api")
    public PageResponse<SomeDto> getAllObjects(
            FilterProperties properties,
            @RequestParam(value = "searchPhrase", defaultValue = "") String searchPhrase,
            @RequestParam(value = "actionType") ActionType actionType) {
        System.out.println(searchPhrase);
        return null;
    }

我设法成功地通过 .param(“ searchphrase”,“ somephrase”,“ somephrase”)成功地通过@requestparams 我似乎找不到传递filterProperties对象的方法,因为它只是一个普通的对象,并且没有注释为param,请求正文或某种属性。

测试:

final MvcResult mvcResult = restServiceMockMvc
                .perform(get(CONTROLLER_BASE_PATH + "/test-api")
                        .param("searchPhrase", "SomePhrase")
                        .param("actionType", String.valueOf(ActionType.EDIT))
                        .requestAttr("properties", filterProperties)
                        .contentType(APPLICATION_JSON_UTF8))
                .andDo(print())
                .andExpect(status().isOk())
                .andReturn();

我尝试使用requestAttr,flashAttr,sessionAttr尝试,但不会打破呼叫,但是API接收一个空的对象以用于FilterProperties。

感谢任何见解!

i am trying to pass a regular object to an api, via the MockMvc library.
Here is the example (Partiucalarly the FilterProperties properties object):

API:

@GetMapping("/test-api")
    public PageResponse<SomeDto> getAllObjects(
            FilterProperties properties,
            @RequestParam(value = "searchPhrase", defaultValue = "") String searchPhrase,
            @RequestParam(value = "actionType") ActionType actionType) {
        System.out.println(searchPhrase);
        return null;
    }

I managed to successfully pass the @RequestParams with .param("searchPhrase", "SomePhrase"), however i cannot seem to find a way to pass the FilterProperties object, since its just a plain object and is not annotated as param, request body or some sort of attribute.

TEST:

final MvcResult mvcResult = restServiceMockMvc
                .perform(get(CONTROLLER_BASE_PATH + "/test-api")
                        .param("searchPhrase", "SomePhrase")
                        .param("actionType", String.valueOf(ActionType.EDIT))
                        .requestAttr("properties", filterProperties)
                        .contentType(APPLICATION_JSON_UTF8))
                .andDo(print())
                .andExpect(status().isOk())
                .andReturn();

I tried with requestAttr, flashAttr, sessionAttr and it does not break the call, however the api receives an empty object for filterProperties.

I appreciate any insights!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘虞初梦 2025-01-29 12:55:54

filterProperties 将被视为 @modelattribute 在其上注释(该规则在此 Table )。

@modelattribute 将启用数据绑定,该数据将尝试从查询参数绑定值,或表单数据或其他形式的字段名称(请参阅 this 有关更多详细信息)to filts> filts Properties

因此,这意味着假设 filterProperties 具有以下字段:

public class FilterProperties {
        
        private String prop1;
        private String prop2;
}

然后,您可以将 MOCKMVC >

mockMvc.perform(get(CONTROLLER_BASE_PATH + "/test-api")
                        .param("searchPhrase", "SomePhrase")
                        .param("actionType", String.valueOf(ActionType.EDIT))
                        .param("prop1", "prop1-value")
                        .param("prop2", "prop2-value"))

将以下值传递到 FilterProperties ::

prop1 = prop1-value
prop2 = prop2-value

FilterProperties will be treated as there are @ModelAttribute annotated on it (mentioned by the rules in this table).

And @ModelAttribute will enable data binding which will try to bind the value from query parameters , or field name of the form data or others (see this for more details) to FilterProperties .

So that means assuming FilterProperties has the following fields :

public class FilterProperties {
        
        private String prop1;
        private String prop2;
}

Then you can configure the MockMvc as

mockMvc.perform(get(CONTROLLER_BASE_PATH + "/test-api")
                        .param("searchPhrase", "SomePhrase")
                        .param("actionType", String.valueOf(ActionType.EDIT))
                        .param("prop1", "prop1-value")
                        .param("prop2", "prop2-value"))

in order to pass the following values to the fields of the FilterProperties :

prop1 = prop1-value
prop2 = prop2-value
你列表最软的妹 2025-01-29 12:55:54

每当您将对象传递到API时,它都必须以一种或另一种方式成为API签名的一部分;我想在这里,您想通过一个get API(高度灰心)通过一个身体,因为GET呼叫本质上是愿意的。

如果如果要测试进行测试

whenever you pass an object to an api, it has to be part of the api signature in one way or another; i guess here you want to pass a body for the GET api(which is highly discouraged),since GET calls are idempotent by nature.

if though if its for testing try passing it as a requestbody and then mock it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文