我有一个使用 MockMvc 的 springBoot 2.1.9.RELEASE 应用程序。
我想知道是否有办法从文件中获取正文内容
mockMvc.perform(post("/hostel")
.content(withBodyFile("hostel.json"))
就像我们可以做的那样
com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder (withBodyFile)
最佳答案
你可以使用类似的东西:
@SneakyThrows
private byte[] fromFile(String path) {
return new ClassPathResource(path).getInputStream().readAllBytes();
}
然后:
.content(fromFile("payload.json")))
请记住,payload.json
文件必须位于 src/test/resources
文件夹下。
关于java - mockMvc,在SpringBoot中从json文件中获取内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58298867/