首页 文章

java.lang.AssertionError:JSON路径期望不同的结果

提问于
浏览
0

我有一个与mockMvc一起使用的Junit测试,它发生了一些奇怪的事情 . 我的测试用例看起来像......

@Test
public void getSignatureData() throws Exception {
    String dataXValues = "[0,5,10,15,20]";
    String dataYValues1 = "[100.0,20.0,30.0,40.0,50.0]";
    String dataYValues2 = "[1.0,2.0,3.0,4.0,5.0]";
    this.mockMvc
    .perform(get("/sources/fmf/actuators/w01.pmv/signatures/1486684800000"))

    .andDo(print())
    .andExpect(status().isOk())
    .andExpect(jsonPath("$.signature.id").value("1486684800000"))
    .andExpect(jsonPath("$.signature.actuatorId").value("w01.pmv"))
    .andExpect(jsonPath("$.signature.operation").value("OPEN"))
        .andExpect(jsonPath("$.signature.timestamp").value("1486684800000"))
        .andExpect(jsonPath("$.signature.ref").value(true))
        .andExpect(jsonPath("$.signature.current").value(false))
        .andExpect(jsonPath("$.signature.valid").value(true))
        .andExpect(jsonPath("$.signature.source").value("A"))
        .andExpect(jsonPath("$.data[0].sensorSource").value("SEMA"))
        .andExpect(jsonPath("$.data[0].sensorType").value("PRESSURE"))
        .andExpect(jsonPath("$.data[0].xValues", is(dataXValues)))
        .andExpect(jsonPath("$.data[0].yValues").value(dataYValues1))
        .andExpect(jsonPath("$.data[1].sensorSource").value("SEMA"))
        .andExpect(jsonPath("$.data[1].sensorType").value("FLOW"))
        .andExpect(jsonPath("$.data[1].xValues").value(dataXValues))
        .andExpect(jsonPath("$.data[1].yValues").value(dataYValues2));
}

我希望它有效,但我得到了这个消息 .

java.lang.AssertionError: JSON path "$.data[0].xValues"
Expected: is "[0,5,10,15,20]"
but: was <[0,5,10,15,20]>
Expected :is "[0,5,10,15,20]"

Actual   :<[0,5,10,15,20]>

有人可以帮我吗?在这种情况下,我使用那些..

import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;

import static org.hamcrest.Matchers . *;

1 回答

  • 0

    我遇到了同样的问题并通过将测试中的预期和实际数组转换为Lists来解决它:

    Arrays.asList()
    

    这为我解决了这个问题 .

相关问题