java.lang.AssertionError:得到了一个值列表,但是与预期的单个值不符合。

huangapple go评论92阅读模式
英文:

java.lang.AssertionError: Got a list of values But instead of the expected single value

问题

以下是翻译好的部分:

在 Spring Boot 应用程序上实现 Java 单元测试用例。

在使用 Java 单元测试框架 Junit 5 比较预期数据和实际数据时,我遇到了以下错误。

**这是我的实现代码,**

@Test
public void testListAddress() throws Exception {

    listAddress.add(new Address(1L, "road 89", "Dhaka", "Manikgonj1", "1800"));
    listAddress.add(new Address(2L, "road 88", "Dhaka1", "Manikgonj2", "1801"));

    Mockito.when(addressServiceImpl.findAllAddresses()).thenReturn(listAddress);
    String url = "/api/v1/address";
    MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(url);
    ResultMatcher ok = MockMvcResultMatchers.status().isOk();
    final ObjectMapper objectMapper = new ObjectMapper();

    String expectedJsonResponse = objectMapper.writeValueAsString(listAddress);

    ResultActions mvcResult = (ResultActions) this.mockMvc.perform(builder).andExpect(ok)
            .andExpect(MockMvcResultMatchers.jsonPath("$.data").value(expectedJsonResponse));

}

我的端点结果如下:

{ 
    "status": true,
    "message": "Address 记录列表!",
    "data": [
        {"id": 1, "street": "road 89", "city": "Dhaka", "state": "Manikgonj1", "zip": "1800"},
        {"id": 2, "street": "road 88", "city": "Dhaka1", "state": "Manikgonj2", "zip": "1801"}
    ]
}

**堆栈跟踪:**

java.lang.AssertionError: 得到了值列表

[
    {"id": 1, "street": "road 89", "city": "Dhaka", "state": "Manikgonj1", "zip": "1800"},
    {"id": 2, "street": "road 88", "city": "Dhaka1", "state": "Manikgonj2", "zip": "1801"}
]

而不是预期的单个值

[
    {"id": 1, "street": "road 89", "city": "Dhaka", "state": "Manikgonj1", "zip": "1800"},
    {"id": 2, "street": "road 88", "city": "Dhaka1", "state": "Manikgonj2", "zip": "1801"}
]
英文:

To implemented java unit test case on spring boot Applications.

I am getting this error when i matched my expected data and actual data using java unit test framework Junit 5.

Here is my implemented code,

    @Test
    public void testListAddress() throws Exception {

        listAddress.add(new Address(1L, "road 89", "Dhaka", "Manikgonj1", "1800"));
        listAddress.add(new Address(2L, "road 88", "Dhaka1", "Manikgonj2", "1801"));

        Mockito.when(addressServiceImpl.findAllAddresses()).thenReturn(listAddress);
        String url = "/api/v1/address";
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(url);
        ResultMatcher ok = MockMvcResultMatchers.status().isOk();
        final ObjectMapper objectMapper = new ObjectMapper();

        String expectedJsonResponse = objectMapper.writeValueAsString(listAddress);

        ResultActions mvcResult = (ResultActions) this.mockMvc.perform(builder).andExpect(ok)
                .andExpect(MockMvcResultMatchers.jsonPath("$.data").value(expectedJsonResponse));

    }

My endpoint results are like:

{ 
"status":true,
"message":"Address record lists!!",
"data":[
       {"id":1,"street":"road 89","city":"Dhaka","state":"Manikgonj1","zip":"1800"}, 
       {"id":2,"street":"road 88","city":"Dhaka1","state":"Manikgonj2","zip":"1801"}
    ]
}

stack trace:

   java.lang.AssertionError: Got a list of values

    [{"id":1,"street":"road 89","city":"Dhaka","state":"Manikgonj1","zip":"1800"},{"id":2,"street":"road 88","city":"Dhaka1","state":"Manikgonj2","zip":"1801"}]

     instead of the expected single value

     [{"id":1,"street":"road 89","city":"Dhaka","state":"Manikgonj1","zip":"1800"},{"id":2,"street":"road 88","city":"Dhaka1","state":"Manikgonj2","zip":"1801"}]

答案1

得分: 1

你可以像这样做:

import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser;
import com.fasterxml.jackson.databind.ObjectMapper;

...

listAddress.add(new Address(1L, "road 89", "Dhaka", "Manikgonj1", "1800"));
listAddress.add(new Address(2L, "road 88", "Dhaka1", "Manikgonj2", "1801"));

JSONParser jsonParser = new JSONParser(JSONParser.DEFAULT_PERMISSIVE_MODE);
ObjectMapper objectMapper = new ObjectMapper();

JSONArray listJson = (JSONArray) jsonParser.parse(objectMapper.writeValueAsString(listAddress));

ResultActions mvcResult = (ResultActions) this
    .mockMvc
    .perform(builder)
    .andExpect(ok)
    .andExpect(MockMvcResultMatchers.jsonPath("$.data").value(Matchers.containsInAnyOrder(listJson.toArray())));
英文:

You can do something like this:

import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser;
import com.fasterxml.jackson.databind.ObjectMapper;

...

listAddress.add(new Address(1L, "road 89", "Dhaka", "Manikgonj1", "1800"));
listAddress.add(new Address(2L, "road 88", "Dhaka1", "Manikgonj2", "1801"));

JSONParser jsonParser= new JSONParser(JSONParser.DEFAULT_PERMISSIVE_MODE);
ObjectMapper objectMapper = new ObjectMapper();

JSONArray listJson = (JSONArray) jsonParser.parse(objectMapper.writeValueAsString(listAddress));

ResultActions mvcResult = (ResultActions) this
.mockMvc
.perform(builder)
.andExpect(ok)
.andExpect(MockMvcResultMatchers.jsonPath("$.data").value(Matchers.containsInAnyOrder(listJson.toArray())));

huangapple
  • 本文由 发表于 2020年8月23日 02:11:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63539533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定