【问题标题】:Spring MVC Test: how to add description to andExpectSpring MVC 测试:如何向 andExpect 添加描述
【发布时间】:2018-01-12 11:15:52
【问题描述】:

在一个测试示例中

@Test
public void aSampleTest() {
    this.mockMvc
      .perform(get("/ultimateQuestion"))
      .andExpect(jsonPath("$.meaningOfLife", is("42"));
}

...是否可以添加(失败的)测试描述,以便失败的测试具有人类可读的消息,而不仅仅是

Expected: is "42"
   but was 24

我的意思是,类似于

assertTrue(boolean condition, String message)

相对

assertTrue(boolean condition)

【问题讨论】:

    标签: spring spring-mvc spring-test hamcrest junit5


    【解决方案1】:

    不,目前没有办法自定义失败“原因”。

    org.springframework.test.util.JsonPathExpectationsHelper.assertValue(String, Matcher<T>) 中硬编码为"JSON path \"" + this.expression + "\"",其中this.expression 是您提供的JsonPath 表达式(例如,在您的示例中为"$.meaningOfLife")。

    【讨论】:

      猜你喜欢
      • 2016-04-14
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多