【问题标题】:Java Spring: JUnit Hamcrest: Expecting CollectionJava Spring:JUnit Hamcrest:期待集合
【发布时间】:2016-04-11 22:04:58
【问题描述】:

我正在我的 Java Spring Web 应用程序中运行我的集成测试套件,但是遇到了以下错误。

有一个单独的项目值:id 的 {id value},在响应中返回。

java.lang.AssrtionError: 1 expectation failed.
JSON path data.id doesn't match.
Expected: a collection containing "BUNDLE_A"
Actual: BUNDLE_A

IntegrationTest.java:

 @Test
  public void testBundle() throws Exception {
    RestAssured.when()
      .get("v1/bundles/{bundleId}", TEST_BUNDLE_ID)
    .then()
      .statusCode(HttpStatus.OK.value())
      .body("data.id", hasItem(TEST_BUNDLE_ID))
      .body("errorCode", nullValue());
  }

【问题讨论】:

    标签: java spring junit hamcrest


    【解决方案1】:

    将您的hasItem 替换为equalTo

     @Test
      public void testBundle() throws Exception {
        RestAssured.when()
          .get("v1/bundles/{bundleId}", TEST_BUNDLE_ID)
        .then()
          .statusCode(HttpStatus.OK.value())
          .body("data.id", equalTo(TEST_BUNDLE_ID))
          .body("errorCode", nullValue());
      }
    

    【讨论】:

    • 我收到一个错误:方法 equalTo(String) 未定义为 IntegrationTest 类型
    • 你导入了吗? import static org.hamcrest.Matchers.equalTo
    • 谢谢,就是这样!
    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 2012-08-10
    • 2010-11-08
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    相关资源
    最近更新 更多