【问题标题】:How to Specify Src/Test/Resources folder as XSD resource path如何将 Src/Test/Resources 文件夹指定为 XSD 资源路径
【发布时间】:2018-06-08 09:59:21
【问题描述】:

我正在使用 java 和 REST Assured 创建自动化 Web 服务测试。

目前,我正在根据 XSD 文件验证收到的 XML 响应架构。

下面是我的代码:

InputStream ValidXsd = Thread.currentThread().getContextClassLoader().getResourceAsStream("BR000AcceptSchema.xsd");

    RestAssured.given()
    .auth()
    .preemptive()
    .basic(theUsername, thePassword)
    .contentType(theContentType)
    .header("Accept",theContentType)
    .body(theXMLBody)
    .when()
    .post(theURL)
    .then()     
    .body(io.restassured.matcher.RestAssuredMatchers.matchesXsd(ValidXsd));

我的问题是这个 XSD 文件当前存储在 src/main/resources 中,但我需要将它移动到 src/test/resources

但是,当我这样做时,测试失败了,因为代码没有拾取 XSD 文件 (BR000AcceptSchema.xsd)。

谁能告诉我如何指定要在 src/test/resources 中查找此 XSD 的代码?

我认为问题与将 XSD 添加到类/构建路径有关。

【问题讨论】:

  • 是测试代码吗(src/test/java)?
  • 是的,src/test/java中的代码测试功能代码
  • 那么它应该是可发现的,我看不出你的代码有什么问题。首先尝试getResource(...)getResources(...) 并检查返回的内容。另一个问题可能是由于某种原因资源没有复制到目标文件夹。如果资源在那里,请检查目标文件夹。如果您在 IDE 中,请检查构建器日志。如果您在 Eclipse 中使用 Maven,请检查 m2 控制台。
  • @lexicore 你好,我试过System.out.println(new File("BR000AcceptSchema.xsd").getAbsolutePath());,然后返回:C:\Users\username\Desktop\Automation Testing\project-anem\BR000AcceptSchema.xsd .我还在项目中搜索了这个 XSD 文件,它出现的唯一位置是 project-name\src\test\resources\US46052project-name\target\classes\主\US46052
  • @lexicore 我已经设法自我诊断问题,XSD 文件包含在 src/test/resources 的子文件夹中!

标签: java xml xsd


【解决方案1】:

XSD 文件存储在 src/test/resources 中的子文件夹中,我忘记在文件路径中指定。

下面的代码现在可以工作了:

InputStream ValidXsd = Thread.currentThread().getContextClassLoader().getResourceAsStream("US46052/BR000AcceptSchema.xsd");

RestAssured.given()
.auth()
.preemptive()
.basic(theUsername, thePassword)
.contentType(theContentType)
.header("Accept",theContentType)
.body(theXMLBody)
.when()
.post(theURL)
.then()     
.body(io.restassured.matcher.RestAssuredMatchers.matchesXsd(ValidXsd));

唯一需要更改的是第一行代码中 XSD 的文件路径

【讨论】:

    猜你喜欢
    • 2018-09-16
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 2015-04-24
    相关资源
    最近更新 更多