简体   繁体   English

如何在MockMvc Sp​​ring Test中匹配对象列表?

[英]How to match List of Objects in MockMvc Spring Test?

I have a MockMVC test that returns a list of objects. 我有一个MockMVC测试,该测试返回对象列表。 I am trying to check this specific property is set correctly. 我正在尝试检查此特定属性的设置是否正确。

However I cannot get it to match using any way, apart from by explicitly adding it. 但是,除了显式添加之外,我无法通过任何方式使其匹配。 KeyPair is a simple pair class with a key value, very similar to an entry of a map. KeyPair是具有键值的简单配对类,非常类似于映射的条目。

     List<KeyPair<String, String>> keyList = User.getContactDetails()
        .entrySet()
        .stream()
        .map( e -> new KeyPair<String, String>( e ) )
        .collect( Collectors.toList() );

      performQueryWithHeaders( get( "/users/1" ) )
            .andExpect( status().isOk() )
            .andExpect( jsonPath( "$.contactDetails" ,
                  is( keyList ) ) );

I have tried a few options 我尝试了一些选择

is( keyList ) 是(keyList)

is( keyList.toString() ) is(keyList.toString())

is( Gson.toJson(keylist ) 是(Gson.toJson(keylist)

is( mapper.writeValueAsString(keyList) (jackson serialisation) is(mapper.writeValueAsString(keyList)(杰克逊序列化)

However keep getting different mismatch messages: 但是,不断收到不同的不匹配消息:

    JSON path "$.contactDetails"
Expected: is "[{\"key\":\"example title\",\"value\":\"example title\"},    {\"key\":\"another example\",\"value\":\"another example\"},{\"key\":\"etc \",\"value\":\"etc \"}]"
     but: was <[{"key":"example title","value":"example title"},{"key":"another example","value":"another example"},{"key":"etc ","value":"etc "}]>

 JSON path "$.contactDetails"
Expected: is <[KeyPair(key=example title, value=example title), KeyPair(key=another example, value=another example), KeyPair(key=etc , value=etc )]>
 but: was <[{"key":"example title","value":"example title"},{"key":"another example","value":"another example"},{"key":"etc ","value":"etc "}]>

 JSON path "$.contactDetails"
Expected: is "[KeyPair(key=example title, value=example title), KeyPair(key=another example, value=another example), KeyPair(key=etc , value=etc )]"
 but: was <[{"key":"example title","value":"example title"},{"key":"another example","value":"another example"},{"key":"etc ","value":"etc "}]>

 JSON path "$.contactDetails"
Expected: is "[{\"key\":\"example title\",\"value\":\"example title\"},{\"key\":\"another example\",\"value\":\"another example\"},{\"key\":\"etc \",\"value\":\"etc \"}]"
 but: was <[{"key":"example title","value":"example title"},{"key":"another example","value":"another example"},{"key":"etc ","value":"etc "}]>

I don't want to write json in the test as seems a bit rubbish / fragile. 我不想在测试中写json,因为看起来有点垃圾/脆弱。 So does anybody know how to map it? 那么有人知道如何映射它吗?

The problem is that JsonPath parse $.contactDetails as a String and you compare it with List<>. 问题是JsonPath将$.contactDetails .contactDetails解析为一个字符串,然后将其与List <>进行比较。 You can transform keyList to string and compare then. 您可以将keyList转换为字符串,然后进行比较。

In case of parsing $.contactDetails to list i can advice a Gson library 在解析$.contactDetails列出的情况下,我可以建议一个Gson

import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;
// ...
Type listType = new TypeToken<List<KeyPair<String, String>>>() {}.getType();
List<KeyPair<String, String>> yourList = new Gson().fromJson(JsonPath.parse("$.contactDetails")), listType);

Try this out 试试看

performQueryWithHeaders( get( "/users/1" ) )
            .andExpect( status().isOk() )
            .andExpect( gson.toJson(jsonPath( "$.contactDetails") ,
                  is( gson.toJson(keyList ) ) );

The issue seems to be as follows jsonPath( "$.contactDetails") return a JsonObject kind of Structure while gson.toJson(keyList ) returns a String which contains the Json with escaping done for quotes. 问题似乎如下所示: jsonPath( "$.contactDetails")返回一种JsonObject结构类型,而gson.toJson(keyList )返回一个包含Json的String,该字符串对转义进行了转义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM