使用 Spring MockMVC 时如何对结果执行 OR 条件?

问题描述 投票:0回答:1

目前我有以下-

.andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath("$.message",
                            org.hamcrest.Matchers.equalTo("storeCode cannot be blank text; storeCode cannot be null")));

如何在上面添加

OR
条件,如下所示(当然,下面不起作用)

.andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath("$.message",
                            org.hamcrest.Matchers.equalTo("storeCode cannot be blank text; storeCode cannot be null").OR.equalTo("storeCode cannot be null; storeCode cannot be blank text")));
spring spring-mvc junit mockito mockmvc
1个回答
0
投票

您可以使用

either
anyOf
oneOf
匹配器。

either

.andExpect(
    MockMvcResultMatchers.jsonPath(
        "$.message",
        Matchers.either(Matchers.equalTo("storeCode cannot be blank text; storeCode cannot be null"))
        .or(Matchers.equalTo("storeCode cannot be null; storeCode cannot be blank text"))));

anyOf

.andExpect(
    MockMvcResultMatchers.jsonPath(
        "$.message",
        Matchers.anyOf(
            Matchers.equalTo("storeCode cannot be blank text; storeCode cannot be null"),
            Matchers.equalTo("storeCode cannot be null; storeCode cannot be blank text"))));

oneOf

.andExpect(
    MockMvcResultMatchers.jsonPath(
        "$.message",
        Matchers.oneOf(
            "storeCode cannot be blank text; storeCode cannot be null",
            "storeCode cannot be null; storeCode cannot be blank text")));

或者使用正则表达式

matchesPattern
:

.andExpect(
    MockMvcResultMatchers.jsonPath(
        "$.message",
        Matchers.matchesPattern(
            "storeCode cannot be blank text; storeCode cannot be null|storeCode cannot be null; storeCode cannot be blank text"))));
© www.soinside.com 2019 - 2024. All rights reserved.