Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否有合适的匹配程序来解析和比较来自MockMvc的Json响应中的LocalDateTime字段_Java_Testing_Jsonpath_Matcher_Mockmvc - Fatal编程技术网

Java 是否有合适的匹配程序来解析和比较来自MockMvc的Json响应中的LocalDateTime字段

Java 是否有合适的匹配程序来解析和比较来自MockMvc的Json响应中的LocalDateTime字段,java,testing,jsonpath,matcher,mockmvc,Java,Testing,Jsonpath,Matcher,Mockmvc,我正在测试SpringBoot控制器的get方法,它提供了在特定时间范围内用base编写的对象 Content-Type: application/json;charset=UTF-8 Cache-Control: no-cache, no-store, max-age=0, must-revalidate { "apiVersion" : "1.0.1", "error" : false, "data" : { "SENT" : [ { "id" : 3,

我正在测试SpringBoot控制器的
get
方法,它提供了在特定时间范围内用base编写的对象

Content-Type: application/json;charset=UTF-8
Cache-Control: no-cache, no-store, max-age=0, must-revalidate

{
  "apiVersion" : "1.0.1",
  "error" : false,
  "data" : {
    "SENT" : [ {
      "id" : 3,
      "phone" : "9111233456",
      "userId" : 683581,
      "sentAt" : "2019-04-02T11:36:16.51",
      "operation" : "RECOVERY_PASSWORD",
      "smsCode" : "2112"
    } ],

我意识到我可以在mockMvc执行并使用对象映射器解析之后获得json,使用一些流和一个断言,但我想知道是否有内置的方法可以使用andExpect()序列生成json

我尝试过Hamcrest日期匹配器,但它无法解析LocalDateTime格式

java.lang.AssertionError:JSON路径“data.SENT[0].sentAt”
预计:日期为“08аПаС2019 19:03:48 614ms+0300”后10天内
但是:是“2019-04-02T11:36:16.51”

我希望能够检查返回数据中的所有对象是否在断言时间范围内

Content-Type: application/json;charset=UTF-8
Cache-Control: no-cache, no-store, max-age=0, must-revalidate

{
  "apiVersion" : "1.0.1",
  "error" : false,
  "data" : {
    "SENT" : [ {
      "id" : 3,
      "phone" : "9111233456",
      "userId" : 683581,
      "sentAt" : "2019-04-02T11:36:16.51",
      "operation" : "RECOVERY_PASSWORD",
      "smsCode" : "2112"
    } ],


我可以看看有没有什么具体的东西。但我不能确定在我的返回数据中是否有任何时间段记录。

我自己制作了匹配器:


import net.minidev.json.JSONArray;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;

import java.time.LocalDateTime;

class CustomMatcher extends BaseMatcher<LocalDateTime> {

    private LocalDateTime from;
    private LocalDateTime to;
    private int misMatchAtIndex;

    CustomMatcher(LocalDateTime from, LocalDateTime to) {
        this.from = from;
        this.to = to;
    }

    @Override
    public boolean matches(Object item) {
        JSONArray rawData = (JSONArray) item;
        for (Object raw : rawData) {
            LocalDateTime parsed = LocalDateTime.parse(raw.toString());
            if (!parsed.isBefore(to) || !parsed.isAfter(from)) {
                misMatchAtIndex = rawData.indexOf(raw);
                return false;
            }
        }
        return true;
    }

    @Override
    public void describeTo(Description description) {
        description.appendText(String.format("All DateTime fields from %s to %s, mismatch at index %d",
                from, to, misMatchAtIndex));
    }
}
 .andExpect(jsonPath("data." + SENT + "[*].sentAt", new CustomMatcher(fromDate, toDate)))