Can MockWebServer use local Json File as Response body?

3.5k views Asked by At

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

1

There are 1 answers

0
mhutti1 On

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)));