开发者社区 问答 正文

mockMvc,在SpringBoot中从json文件获取内容

我有一个使用MockMvc的springBoot 2.1.9.RELEASE应用程序。

我想知道是否有一种方法可以从文件中获取正文内容

mockMvc.perform(post("/hostel") .content(withBodyFile("hostel.json")) 就像我们可以做的

com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder (withBodyFile)

展开
收起
小六码奴 2019-10-09 19:29:16 1126 分享 版权
1 条回答
写回答
取消 提交回答
  • 您可以使用类似:

    @SneakyThrows private byte[] fromFile(String path) { return new ClassPathResource(path).getInputStream().readAllBytes(); } 接着:

    .content(fromFile("payload.json"))) 请记住,该payload.json文件必须位于该src/test/resources文件夹下。

    2019-10-09 19:29:41
    赞同 展开评论