2

How can I use local Json File as response body in MockWebServer? Thank you!

LunarS
  • 95
  • 2
  • 10

1 Answers1

2

You can stream the json file to a byte array. Using org.apache.commons.io.IOUtils to save time, then pass this as a String to a new MockResponse body.

InputStream jsonStream = MyClass.class.getClassLoader().getResourceAsStream("file.json");
byte[] jsonBytes = IOUtils.toByteArray(jsonStream);
mockWebServer.enqueue(new MockResponse().setBody(new String(jsonBytes)));
mhutti1
  • 65
  • 8