Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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 Spring JUnit测试-整数的JSONPath不匹配_Java_Spring_Spring Mvc_Junit - Fatal编程技术网

Java Spring JUnit测试-整数的JSONPath不匹配

Java Spring JUnit测试-整数的JSONPath不匹配,java,spring,spring-mvc,junit,Java,Spring,Spring Mvc,Junit,对于基于Spring的Rest控制器,我在JUnit测试中有以下断言 .andExpect(jsonPath("$..terminationPoint.downstreamSpeedKbps", is(Integer.valueOf(500)))); 此断言是针对以下有效负载进行的: { "result":[ { "terminationPoint":{ "downstreamSpeedKbps":500

对于基于Spring的Rest控制器,我在JUnit测试中有以下断言

.andExpect(jsonPath("$..terminationPoint.downstreamSpeedKbps", is(Integer.valueOf(500))));
此断言是针对以下有效负载进行的:

{  
   "result":[  
      {  
         "terminationPoint":{  
            "downstreamSpeedKbps":500
         }
      }
   ]
}
当断言运行时,我得到以下错误:

java.lang.AssertionError: JSON path "$..terminationPoint.downstreamSpeedKbps"
Expected: is <500> but: was <[500]>
java.lang.AssertionError:JSON路径“$…terminationPoint.downstreamSpeedKbps”
预期:是但:是

知道这些方括号来自何处或者它们是什么意思吗?

问题是:表达式
$…
返回一个
数组。。您也可以从错误消息中识别它,您可以执行以下操作:


$…terminationPoint.downstreamSpeedKbps[0]
$..
将执行深度扫描并返回json中所有值的数组。 请改用特定索引:

.andExpect(jsonPath("$.result[0].terminationPoint.downstreamSpeedKbps", 
                    is(Integer.valueOf(500))));

使用jsonpath并检查结果

这将返回一个空数组,并导致以下错误:
预期:is but:was
第一个解决方案有效,第二个返回以下错误:
预期:is[]but:was
@SamanthaCatania您是对的。删除第二个解决方案。只要结果是json数组,更好的方法就是使用结果的索引