У меня есть GET API, использующий java .ws.rs, например:
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getMyFile() throws AbsoluteException {
StreamingOutput outputStream;
Client client = ClientBuilder.newClient();
try {
final InputStream responseStream = client.target(myFileUrl).request().get(InputStream.class);
outputStream = new StreamingOutput() {
@Override
public void write(OutputStream out) throws IOException {
int length;
byte[] buffer = new byte[downloadBufferSize];
while ((length = responseStream.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
out.flush();
responseStream.close();
}
};
} catch (Exception ex) {
}
return Response
.ok(outputStream, MediaType.APPLICATION_OCTET_STREAM)
.header("content-disposition", "attachment; filename = " + myFile)
.build();
}
Я не знаю, как написать для него unitTest, используя Mockito или любые другие. Мой метод испытаний, как показано ниже, но он не работает:
@Test
public void getMyFile_Success() throws IOException {
final Client mockClient = Mockito.mock(Client.class);
final File initialFile = new File("src/test/resources/TestFile.txt");
final InputStream targetStream =
new DataInputStream(new FileInputStream(initialFile));
Mockito.when(mockClient.target("myUrl")
.request().get(InputStream.class))
.thenReturn(targetStream);
Invocation.Builder request = target(CONTEXT_ROOT).request();
Response response = request.get(Response.class);
assertThat(response.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
}